OData $ format系统查询选项导致错误请求400 [英] OData $format system query option causes Bad Request 400

查看:112
本文介绍了OData $ format系统查询选项导致错误请求400的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的基于反射的OData示例,运行正常,并在按指示使用Accept标头时生成json.但是,我无法使其与$ format = json参数一起使用.每当我添加该参数时,我都会收到错误请求.据此,它似乎应该起作用:链接文字

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

请注意,其他系统查询选项(例如$ select)也可以正常工作.这是通过VS2010运行的.Net 4.

Note that other system query options like $select do work okay. This is .Net 4 running via VS2010.

推荐答案

即使OData Spec声明支持,对.NET 4 WCF数据服务使用现成的$format=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.

首先,感觉有点棘手的解决方案是构建一个HttpHandler来拦截您的请求,读取$format=json querystring参数,然后向您的请求添加一个accepts标头(同时删除有问题的$format=json参数).这在此博客文章.

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.

听起来更好一点的第二种解决方案是用[JSONPSupportBehavior]属性装饰数据服务.这更有意义,并且易于实现(因为您不必构建HttpHandler).以下是一些有用的链接:

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:

  • 下载链接 [JSONPSupportBehavior]的源代码 属性(是的,您必须构建 它-我还没有找到一个编译 下载).
  • 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).

我喜欢属性方法,我只是希望它不是从CodePlex下载的……听起来还不支持.但这只是我的意见.

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.

老实说,如果您有控制权,最好的方法就是在您的application/json请求中添加一个accepts标头,您的服务将自动返回JSON格式的结果.

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.

我希望这会有所帮助.

这篇关于OData $ format系统查询选项导致错误请求400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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