具有实体框架4.0的WCF Rest服务返回json响应 [英] WCF Rest services with entity framework 4.0 returns json response

查看:77
本文介绍了具有实体框架4.0的WCF Rest服务返回json响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试使用实体框架4.0创建启用json的wcf服务.

当我使用
以XML格式返回响应时

 [WebInvoke(Method = " ,UriTemplate = " ,BodyStyle = WebMessageBodyStyle.WrappedRequest,RequestFormat = WebMessageFormat.Xml,ResponseFormat = WebMessageFormat.Xml)]] 



该方法运行良好,并提供了数据库中的数据.但是当我使用
将相同的方法转换为返回json格式时

 [WebInvoke(Method = " ,UriTemplate = " ,BodyStyle = WebMessageBodyStyle.WrappedRequest,RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)] 


我什么都没有.浏览器显示默认页面,要求您检查我的网址.

我也创建了自我跟踪实体..

任何人都可以帮助我解决这个问题吗?

请提前帮忙..请尽快提供帮助...

解决方案

请先检查您的绑定.当您使用jSon作为响应类型时,请使用webHttpBinding而不是basicHttpBinding.我的端点行为...

< webhttp defaultoutgoingresponseformat = "  automaticformatselectionenabled = " /> 



现在运行良好...感谢您考虑..

为了将list< students>()序列化为Json,我使用了JavaScriptSerializer

 公共 字符串 getStudentArray()
       {
           使用(StudentsEntities ctx =  StudentsEntities())
           {
                var  oSerializer =  System.Web.Script.Serialization.JavaScriptSerializer();
               字符串 sJSON = oSerializer.Serialize(((来自 s  in  ctx.学生选择 s).ToList());
               返回 sJSON;
           }
       }


hi,

I am trying to create json enabled wcf services with entity framework 4.0.

when i return the response in XML format using

[WebInvoke(Method = "GET", UriTemplate = "/getStudents", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]



the method is working fine and giving the data from the database. but when i convert the same method to return json format using

[WebInvoke(Method = "GET", UriTemplate = "/getString", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 


i am not getting any thing. browser displays the default page which asks to check my url.

i have created self tracking entity also..

can any one help me with this problem???

thnks in advance.. please help soon...

解决方案

Check you binding first. Use webHttpBinding instead of basicHttpBinding, when you are using jSon as response type.


hi i got the solution for my problem...i just need to make some changes in web.config file for my endpoint behavior...

<webhttp defaultoutgoingresponseformat="Json" automaticformatselectionenabled="True" />



and now it is working well...Thnks for your consideration..

and in order to serialize the list<students>() into Json i used JavaScriptSerializer

public string getStudentArray()
       {
           using (StudentsEntities ctx = new StudentsEntities())
           {
               var oSerializer = new  System.Web.Script.Serialization.JavaScriptSerializer();
               string sJSON = oSerializer.Serialize((from s in ctx.Students select s).ToList());
               return sJSON;
           }
       }


这篇关于具有实体框架4.0的WCF Rest服务返回json响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆