0
votes

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?

3
Have you read up on 406 errors? You haven't mentioned anything about your accept headers in the request, which is what 406 is pointing at. Post to ReferenceJonesopolis
Yes I checked this, from what people have said, they seem to infer that your browser should try to render it out regardless of whether you have included it or not. MVC should be returning json or xml. I added in the Accept header with the various content-types that you can get from oData but to no availMark

3 Answers

2
votes

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).

2
votes

For me , the problem was the entity I was returning was not mapped to the model mapped to the "GetEdmModel" edmModel, i.e. the data returned after the call was not getting correctly mapped to the resource model.

PS: When I was inheriting from ApiController instead of ODataController, it was working fine. So I also had the same confusion as to why it was having 406 error when the route was properly hitting.

Hope, this helps. It was something I faced.

0
votes

I had similar implementation where I had a base class that inherited ODataController with a get method like:

public IHttpActionResult Get()
{
    Verify();
    return Ok(GetDataInstance());
}

where GetDataInstance was a virtual method that returned an IQueryable. I was able to eliminate the 406 error by changing the GetDataInstance() to return IQueryable<EntityClassName>.