从的WebAPI返回PageResult没有格式化为的OData [英] Returning PageResult from WebAPI not formatting as OData
问题描述
我试图从的WebAPI RC一个的WebAPI为基础的应用程序迁移到发行版本。这需要在一些查询参数,并返回ATOM格式的OData。因为它是一个正在运行的服务,我需要保持当前的行为。
I'm attempting to migrate a WebAPI-based app from WebAPI RC to the release version. It takes in some query parameters, and returns ATOM-formatted OData. Since it's a running service, I need to maintain the current behavior.
我已经改变了API方法返回一个 PageResult< T>
我在里面的数据。根据本 <一href="http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options"相对=nofollow>支持OData的查询选项 的文章在MSDN上这应该是所有我需要做的,但它不工作。我得到的结果,但它总是格式化为JSON。我试着改变接受
请求头应用程序/原子+ XML
,但它似乎并没有使任何区别。
I've changed the API methods to return a PageResult<T>
with my data in it. According to the Supporting OData Query Options article on MSDN that should be all I need to do, but it's not working. I get the result, but it's always formatted as JSON. I've tried changing the Accept
request header to application/atom+xml
, but it doesn't seem to make any difference.
我也试着将我的 WebApiConfig
以下行没有明显的影响:
I've also tried adding the following lines in my WebApiConfig
to no apparent effect:
configuration.EnableQuerySupport();
configuration.Formatters.InsertRange(0, ODataMediaTypeFormatters.Create());
我试图清理掉现有的格式化,只是为了看看会发生什么。我只是拿回 406不可接受
错误。因此,它似乎是可能的OData的格式化没有报告说,他们可以处理请求/响应?
I tried clearing out the existing formatters, just to see what would happen. I just get back 406 Not Acceptable
errors. So it seems like perhaps the OData formatters are not reporting that they can handle the request/response?
推荐答案
请通过这个博客帖子,了解如何启用OData的:
Please go through this blog post to learn about enabling OData:
<一个href="http://blogs.msdn.com/b/webdev/archive/2013/01/29/getting-started-with-asp-net-webapi-odata-in-3-simple-steps.aspx" rel="nofollow">http://blogs.msdn.com/b/webdev/archive/2013/01/29/getting-started-with-asp-net-webapi-odata-in-3-simple-steps.aspx
您错过了OData的路线 - 路线所需的格式化工作
You're missing an OData route - the route is required for the formatter to work.
这篇关于从的WebAPI返回PageResult没有格式化为的OData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!