手柄的OData / EntitySet的/键/导航 [英] Handle Odata /entityset/key/navigation

查看:183
本文介绍了手柄的OData / EntitySet的/键/导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个基于从ODataSamples主ODATA例子DynamicEdmModelCreation项目一期工程。

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

我们尝试例如:

/ ODATA /酒店 - >确定

/odata/Hotels -> OK!

/ ODATA /酒店(1) - >确定

/odata/Hotels(1) -> Ok!

/ ODATA /酒店(1)/间 - >响应:

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

没有路由惯例被发现选择与模板'〜/ EntitySet的/键/导航。

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

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

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;
    }
}

我们认为,问题可能出在挑选一的WebAPI,因为,因为我们使用的是通用的签名将处理请求的正确方法的 IEdmEntityObject获取(字符串键)的。

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()方法,获取(字符串键) 。其结果是:

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


  1. / ODATA /酒店是确定的,因为请求可以路由到的get()按照约定的方法

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

/ ODATA /酒店(1)是确定的,因为请求可以路由到获取(字符串键)按照约定。

/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 /酒店(1)/房

由于,有没有方法回应〜/ EntitySet的/键/导航,网页API的OData无法找到你的控制器的方法,所以它抛出上述错误消息。

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.

此外,我还创建常规导航属性访问的路径 SelectAction MatchAllRoutingConvention 。见提交:
https://github.com/xuzhg/ODataSamples/commit/200c4abd346f14ad258df2b36e8687f8ab7d85e7

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.

感谢。

山姆

这篇关于手柄的OData / EntitySet的/键/导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆