Search code examples
c#routesodataasp.net-web-apimaproute

How to declare a parameter as prefix on OData


Using a normal ApiController in WebApi, I made the following code to define a dynamic map route template for every ApiControllers:

config.Routes.MapHttpRoute(
        name: "Sample",
        routeTemplate: "{sessionId}/{controller}"
     );

I want to achieve a similar behaviour but for every ODataController. I've tried the following code, but it doesn't work:

 config.MapODataServiceRoute(
        routeName: "HSODataRoute",
        routePrefix: "{sessionId}/",
        model: GetEdmModel());

Any idea how this is made in OData? I'm kinda new to it and the internet is lack of information about this.

To be more specific: {sessionId} isn't supposed to be a constant, but a parameter with a Guid value.


Solution

  • After a few tests I've found out that declaring MapODataServiceRoute is not enough! You need also to add MapHttpRoute also, since ODataController derives from ApiController

    config.Routes.MapHttpRoute(
        name: "Sample",
        routeTemplate: "{sessionId}/{controller}"
     );
    
    config.MapODataServiceRoute(
        routeName: "HSODataRoute",
        routePrefix: "{sessionId}/",
        model: GetEdmModel());
    

    I discovered this because after I removed MapHttpRoute, I've started to get 404 not found, and when I added MapHttpRoute the resource could be found.

    UPDATE:

    The final solution that I've come up to solve this issue was posted here: Pass Parameters in OData WebApi Url.