Returning PageResult from WebAPI not formatting as

2019-06-25 10:18发布

问题:

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.

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.

I've also tried adding the following lines in my WebApiConfig to no apparent effect:

configuration.EnableQuerySupport();
configuration.Formatters.InsertRange(0, ODataMediaTypeFormatters.Create());

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?

回答1:

Please go through this blog post to learn about enabling OData:

http://blogs.msdn.com/b/webdev/archive/2013/01/29/getting-started-with-asp-net-webapi-odata-in-3-simple-steps.aspx

You're missing an OData route - the route is required for the formatter to work.