Search code examples
c#asp.net-web-apiasp.net-mvc-5odata

WebApi oData returning 406


banging my head against a wall with this one. I have an oDataController that returns a view model entity, but everytime I call it, I get a 406 client side. Building this in .Net MVC 5 oData v4 with Web API 2.

When I step through the code, I can see that the routing is working as my function is hit. I can also get the $metadata without issue.

I have checked that the return value (IQueryable<>) is valid server side and does return results so something is happening after my function exits and before the response is received by the client (Postman).

All the threads I have seen that relate to the 406 seem to indicate its a routing issue but mine can't be because the function is being hit.

Any ideas?


Solution

  • So I think I have found the issue, went back to basics and managed to get both web api and odata controller working nicely. Just incase it helps anyone else, I added an oData controller first and made sure that worked in isolation. Then I added a web api controller with the same name (deliberately). In this case, the odata worked but the routing to the api controller did not (the api calls routed to the odata controller which returned a 406). I then added a RoutePrefix attribute to both controller and both controllers then worked. I then attempted to add a little complexity into the app to replicate what I had in my other project, that being controller inheritance, this is where it falls down. Incase you are wondering why I use controller inheritance, its because I have generic controllers that accept type arguments (which I know is a specialist use case). Either way, its the inheritance that is confusing the routing/response, even though it is routing through correctly to the correct end points in my app (as I have a custom ApiSelector implementation).