Search code examples
c#asp.netasp.net-web-apiodata

Handle Odata /entityset/key/navigation


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


Solution

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

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

    2. /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:

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

    Thanks.

    Sam