OData $format 系统查询选项导致错误请求 400
我有一个非常简单的基于反射的 OData 示例,当我按照指示使用 Accept 标头时,它运行良好并生成 json。但是,我无法让它与 $format=json 参数一起使用。每当我添加该参数时,我都会收到错误请求。据此,它似乎应该工作: 链接文本
请注意,其他系统查询选项(例如 $select)也可以正常工作。这是通过 VS2010 运行的 .Net 4。
I have a very simple Reflection-based OData sample the runs fine and generates json when I use the Accept header as indicated. However, I cannot get it to work with the $format=json parameter. Whenever I add that parameter, I get Bad Request. According to this, it seems like it should work: link text
Note that other system query options like $select do work okay. This is .Net 4 running via VS2010.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使 OData 规范表示支持,对 .NET 4 WCF 数据服务使用开箱即用的
$format=json
也是行不通的。我不确定为什么微软不直接支持它。但对于这种情况有两种解决方法——一种感觉有点老套,另一种则有一定道理。首先,感觉有点hacky的解决方案是构建一个HttpHandler来拦截您的请求,读取
$format=json
查询字符串参数,然后向您的请求添加一个accept头(同时删除有问题的>$format=json
参数)。 这篇博文。第二种解决方案听起来好一点,是使用
[JSONPSupportBehavior]
属性来装饰您的数据服务。这更有意义并且更容易实现(因为您不必构建 HttpHandler)。以下是一些有用的链接:它。
[JSONPSupportBehavior]
属性(是的,你必须构建
它——我还没有找到编译好的
下载)。
我喜欢属性方法,我只是希望它不是从 CodePlex 下载......它只是听起来还不支持。但这只是我的意见。
老实说,如果您有控制权,最好的方法就是在您的
application/json
请求中添加 Accept 标头,您的服务将自动返回 JSON 格式的结果。我希望这有帮助。
Using
$format=json
out of the box against a .NET 4 WCF Data Service will not work even though the OData Spec says it's supported. I'm not sure exactly why Microsoft does not support it directly. But there are two workarounds to this situation - one feels a little hacky, and the other makes some sense.First, the solution that feels a little hacky is to build an HttpHandler that intercepts your request, reads the
$format=json
querystring parameter and then adds an accepts header to your request (while removing the offending$format=json
parameter). This is described in this blog post.The second solution, which sounds a little better, is to decorate your data service with a
[JSONPSupportBehavior]
attribute. This makes a little more sense and is a little easier to implement (since you don't have to build an HttpHandler). Here are some useful links:it.
[JSONPSupportBehavior]
attribute (yes, you'll have to build
it -- I haven't found a compiled
download).
I like the attribute approach, I just wish it wasn't a download off CodePlex...it just doesn't sound that supported yet. But that's just my opinion.
Honestly, if you have control, the best approach is just to add an accepts header to your request of
application/json
, and your service will automatically return JSON formatted results.I hope this helps.
任何遇到此问题的人...您现在可以使用 WCF 数据服务工具包 并从 ODataService 而不是 DataService 继承来自动启用此功能。
Anyone who comes across this... You can now use WCF Data Services Toolkit and inherit from ODataService rather than DataService to automatically enable this functionality.