ADO.NET Data Service如何直接支持用Json格式返回数据

今天在介绍WCF及其在项目开发中应用时,再次提到了ADO.NET Data Service。关于这个主题,其实之前我写过不少内容,有兴趣可以参考

http://zzk.cnblogs.com/s?w=blog%3Achenxizhang%20ado.net%20data%20service

 

同时,微软有一篇非常详细的文档

http://msdn.microsoft.com/en-us/library/cc907912.aspx

 

我们知道,ADO.NET Data Service是支持两种数据格式的:xml和json.默认是xml(具体来说是atom格式)

如下面的效果

image

那么,如何让这些结果用json格式返回呢?

 

比较遗憾的是,我们暂时不能直接通过设置就完成这个需求。但是有一个开源的项目,提供了一个解决方案,如下

http://code.msdn.microsoft.com/DataServicesJSONP

The net effect is that if you include this code in your project, you just need to add a single attribute to your Data Service to make it support JSONP:
[JSONPSupportBehavior]
public class SampleService : DataService
{
// your service code here...
}
Once that's in place you can use JSONP by adding $format and $callback to URLs, for example:
http:// /SampleService.svc/People?$format=json&$callback=cb
Of course, you can still use all the other Data Services URL options in addition to these.

 

使用起来很简单,他是写好了一个Behavior的Attribute,我们只需要在有关的类型上面加上即可

image

 

然后,在请求地址中添加$format=json这样的字样即可

http://localhost:1142/OrderService.svc/Customers('ALFKI')/Orders?$format=json

 

返回的json数据,大致如下

image

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值