Skip to main content
 首页 » 编程设计

c#之处理 Odata/entityset/key/navigation

2024年02月05日10飞鱼

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

我们已经设置了一个路由约定来处理对这个特定 Controller 的所有请求:

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

我们举个例子

/odata/Hotels -> 确定!

/odata/Hotels(1) -> 好的!

/odata/Hotels(1)/Room -> 响应:

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

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

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(string key)。

请您参考如下方法:

在您的 Controller 中,只有两个方法名为 Get()、Get(string key)。结果是:

  1. /odata/Hotels 可以,因为请求可以按照惯例路由到 Get() 方法。

  2. /odata/Hotels(1) 可以,因为请求可以按照约定路由到 Get(string key)。

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

/odata/Hotels(1)/Room

由于没有响应~/entityset/key/navigation的方法,Web API OData在你的controller中找不到方法,所以抛出上面的错误信息。

我的尝试:

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

谢谢。

山姆