OData $format system query option causes Bad Request 400

半城伤御伤魂 提交于 2019-12-23 13:24:29

问题


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.


回答1:


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:

  • Blog post describing how to use it.
  • Link to download the source code for the [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.




回答2:


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.



来源:https://stackoverflow.com/questions/3604188/odata-format-system-query-option-causes-bad-request-400

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!