2
votes

for a WebAPI 2 OData solution we need to be able to map the following route-type:

~/odata/1234erp/Products meaning a value prefixes the Controller (or Entityset) segment. Ive got my route set up as follows:

    config.Routes.MapODataRoute(
        routeName: "odata",
        routePrefix: "odata/{tableid}/{controller}", 
        model: ModelBuilder.GetEdmModel(), 
        pathHandler: new DefaultODataPathHandler(),
        routingConventions: conventions);

This makes the SelectAction method in my CustomControllerRoutingConvention get hit (the SelectController does not get hit) but the odataPath parameter counts 0 segments, where i'd hope it would recognize something like ~/tableid/entityset/

Now i can probably solve this using something like

controllerContext.RequestContext.RouteData.Values["tableid"]

but is there a better (typesafer?) solution for this?

EDIT: Also, since ODataPath path = Request.GetODataPath() returns 0 segments, it's no longer possible to derive the EdmType neccessary for ODataQueryOptions...

1

1 Answers

1
votes

Taking off the {controller} segment of the routePrefix like this: "odata/{tableid} seems to solve this. Now i can direct to the correct controller like this

    if (firstSegment != null && firstSegment is EntitySetPathSegment)
    {
        return firstSegment.ToString();
    }

And in that controller i can get the tableid as a parameter: Get(string tableid)

Better yet,

ODataPath path = Request.GetODataPath();
IEdmType edmType = path.EdmType;

returns the correct EdmType...