4
votes

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
Have you installed the Web API OData components? These are available through NuGet.DaveB
Yes, I grabbed the latest from NuGet yesterday.Brian Reischl
Have you added the Queryable ([Queryable(AllowedQueryOptions = AllowedQueryOptions.All)] ) attribute to your controller method and set it's return type to IQueryable<T>? Maybe you could post your controller and global.asax code.DaveB
I'm not returning an IQueryable<T> because I need to return a $skiptoken, and my understanding is that you can't do that with an IQueryable<T>. Hence use of PageResult<T> instead. My function code is irrelevant, lots of proprietary data access. I can repro the problem with a dummy method that just returns new PageResult<string>(new List<string>(), null, null)Brian Reischl

1 Answers

1
votes

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.