简体   繁体   English

WebApi oData 返回 406

[英]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.我有一个 oDataController 返回一个视图模型实体,但每次调用它时,我都会得到一个 406 客户端。 Building this in .Net MVC 5 oData v4 with Web API 2.使用 Web API 2 在 .Net MVC 5 oData v4 中构建它。

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.我也可以毫无问题地获得 $metadata 。

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).我已经检查过返回值 (IQueryable<>) 是有效的服务器端并且确实返回结果,所以在我的函数退出之后和客户端(邮递员)收到响应之前发生了一些事情。

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.我看到的所有与 406 相关的线程似乎都表明它是一个路由问题,但我的不能,因为该函数正在被命中。

Any ideas?有任何想法吗?

So I think I have found the issue, went back to basics and managed to get both web api and odata controller working nicely.所以我想我已经找到了这个问题,回到基础并设法让 web api 和 odata 控制器都很好地工作。 Just incase it helps anyone else, I added an oData controller first and made sure that worked in isolation.以防万一它对其他人有帮助,我首先添加了一个 oData 控制器并确保它可以独立工作。 Then I added a web api controller with the same name (deliberately).然后我添加了一个同名的 web api 控制器(故意)。 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).在这种情况下,odata 工作但路由到 api 控制器没有(api 调用路由到返回 406 的 odata 控制器)。 I then added a RoutePrefix attribute to both controller and both controllers then worked.然后我向两个控制器添加了一个 RoutePrefix 属性,然后两个控制器都可以工作。 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).无论哪种方式,它的继承都会混淆路由/响应,即使它正确地路由到我的应用程序中的正确端点(因为我有一个自定义的 ApiSelector 实现)。

For me , the problem was the entity I was returning was not mapped to the model mapped to the "GetEdmModel" edmModel, ie the data returned after the call was not getting correctly mapped to the resource model.对我来说,问题是我返回的实体没有映射到映射到“GetEdmModel”edmModel 的模型,即调用后返回的数据没有正确映射到资源模型。

PS: When I was inheriting from ApiController instead of ODataController, it was working fine. PS:当我从 ApiController 而不是 ODataController 继承时,它工作正常。 So I also had the same confusion as to why it was having 406 error when the route was properly hitting.所以我也有同样的困惑,为什么当路线正确命中时它会出现 406 错误。

Hope, this helps.希望这可以帮助。 It was something I faced.这是我面临的事情。

I had similar implementation where I had a base class that inherited ODataController with a get method like:我有一个类似的实现,我有一个基类,它继承了ODataController的 get 方法,例如:

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

where GetDataInstance was a virtual method that returned an IQueryable .其中GetDataInstance是一个返回IQueryable的虚拟方法。 I was able to eliminate the 406 error by changing the GetDataInstance() to return IQueryable<EntityClassName> .通过将GetDataInstance()更改为返回IQueryable<EntityClassName>我能够消除 406 错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM