处理Odata / entityset / key / navigation(Handle Odata /entityset/key/navigation)

编程入门 行业动态 更新时间:2024-10-14 20:21:25
处理Odata / entityset / key / navigation(Handle Odata /entityset/key/navigation)

我们有一个基于ODataSamples-master odata示例的DynamicEdmModelCreation项目的项目。

我们设置了一个路由约定来处理对这个特定控制器的所有请求:

[HttpGet] [EnableQuery] public EdmEntityObjectCollection Get() { ... } [EnableQuery] public IEdmEntityObject Get(string key) { ... }

我们试着举个例子

/ odata /酒店 - >好的!

/ odata /酒店(1) - >好的!

/ odata /酒店(1)/房间 - >回复:

没有找到路由约定来为模板'〜/ entityset / key / navigation'选择OData路径的操作。

我们看到路由约定很好地处理请求并将其重定向到我们的控制器但没有执行任何方法。 路由公约是:

public class MatchRoutingConventionService : IODataRoutingConvention { public string SelectAction( ODataPath odataPath, HttpControllerContext controllerContext, ILookup<string, HttpActionDescriptor> actionMap) { return null; } public string SelectController(ODataPath odataPath, HttpRequestMessage request) { return (odataPath.Segments.FirstOrDefault() is EntitySetPathSegment) ? "HamdleAll" : null; } }

我们认为问题可能在WebApi中使用了正确的方法来处理请求,因为我们使用的是通用签名IEdmEntityObject Get(字符串键)

We have a project based on the DynamicEdmModelCreation project from ODataSamples-master odata examples.

We have set a Routing Convention that handle all request to this specific controller:

[HttpGet] [EnableQuery] public EdmEntityObjectCollection Get() { ... } [EnableQuery] public IEdmEntityObject Get(string key) { ... }

We try for example

/odata/Hotels -> OK!

/odata/Hotels(1) -> Ok!

/odata/Hotels(1)/Room -> Response:

No routing convention was found to select an action for the OData path with template '~/entityset/key/navigation'.

Debuging we see that the route convention handle well the request and redirect it to our controller but no method is executed. The Routing Convention is:

public class MatchRoutingConventionService : IODataRoutingConvention { public string SelectAction( ODataPath odataPath, HttpControllerContext controllerContext, ILookup<string, HttpActionDescriptor> actionMap) { return null; } public string SelectController(ODataPath odataPath, HttpRequestMessage request) { return (odataPath.Segments.FirstOrDefault() is EntitySetPathSegment) ? "HamdleAll" : null; } }

We think the problem may be in WebApi chossing the correct method that will handle the request because since we are using the generic signature IEdmEntityObject Get(string key).

最满意答案

在您的控制器中,只有两个名为Get(), Get(string key) 。 结果是:

/odata/Hotels没关系,因为请求可以按惯例路由到Get()方法。

/odata/Hotels(1) ,因为请求可以按惯例路由到Get(string key) 。

但是 ,您不会创建其他方法来响应其他请求,例如请求示例:

/odata/Hotels(1)/Room

由于没有方法响应~/entityset/key/navigation ,Web API OData无法在控制器中找到方法,因此会抛出上述错误消息。

我的尝试:

我在https://github.com/xuzhg/ODataSamples/commit/289658ab5d7bb50c6270400946990c27809d44bc中创建了一个简单的方法

此外,我还在SelectAction的MatchAllRoutingConvention创建了一般的导航属性访问路径。 请参阅提交: https : //github.com/xuzhg/ODataSamples/commit/200c4abd346f14ad258df2b36e8687f8ab7d85e7

您可以修改它以满足您的要求。 希望它可以帮到你。

谢谢。

山姆

In your controller, there're only two methods named Get(), Get(string key). The result is:

/odata/Hotels is ok because the request can route to Get() method by convention.

/odata/Hotels(1) is ok because the request can route to Get(string key) by convention.

However, you doesn't create other methods to response other requests, such as for the request example:

/odata/Hotels(1)/Room

Owing that there's no methods responding to ~/entityset/key/navigation, Web API OData can't find a method in your controller, so it throws the above error message.

My try:

I create a simple method in https://github.com/xuzhg/ODataSamples/commit/289658ab5d7bb50c6270400946990c27809d44bc

Besides, I also create the general navigation property access route in SelectAction the MatchAllRoutingConvention. See the commit: https://github.com/xuzhg/ODataSamples/commit/200c4abd346f14ad258df2b36e8687f8ab7d85e7

You can modify it to meet your requirement. Hope it can help you.

Thanks.

Sam

更多推荐

本文发布于:2023-07-15 17:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1116661.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:entityset   Odata   key   Handle   navigation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!