4

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?

Brian Reischl
  • 7,216
  • 2
  • 35
  • 46
  • Have you installed the Web API OData components? These are available through NuGet. – DaveB Mar 27 '13 at 19:28
  • Yes, I grabbed the latest from NuGet yesterday. – Brian Reischl Mar 27 '13 at 19:34
  • Have you added the `Queryable` ([Queryable(AllowedQueryOptions = AllowedQueryOptions.All)] ) attribute to your controller method and set it's return type to `IQueryable`? Maybe you could post your controller and global.asax code. – DaveB Mar 27 '13 at 19:47
  • I'm not returning an `IQueryable` because I need to return a $skiptoken, and my understanding is that you can't do that with an `IQueryable`. Hence use of `PageResult` 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(new List(), null, null)` – Brian Reischl Mar 27 '13 at 20:10

1 Answers1

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.

Youssef Moussaoui
  • 12,187
  • 2
  • 41
  • 37
  • Unfortunately `MapODataRoute()` doesn't let me define any defaults, which I need to do for legacy reasons. My controllers also don't conform to the pattern, again for legacy reasons. Is there any way I can get the OData formatters without rewriting everything? – Brian Reischl Mar 27 '13 at 20:48
  • Well, that was the solution, but it required basically rebuilding the entire application. – Brian Reischl Mar 28 '13 at 21:32
  • Is this still the "accepted" way you did it? This is AN answer that i have found but, like you said... there are reasons why we would like to use map routes – O'Mutt Feb 13 '14 at 18:00