OData v4 Routing Prefix?
Asked Answered
A

1

9

I have a side-by-side Web API 2.2 APIController and OData v4 ODataController. My APIController uses routing attributes internally like this (there are no predefined routing defaults):

  [RoutePrefix("api")]
  public class MyController : ApiController
  {
    [HttpGet]
    [Route("My")]
    public IHttpActionResult Get()
    {
      //Code Here
    }

    [HttpGet]
    [Route("My")]
    public IHttpActionResult Get([FromUri] String mykey)
    {
      //Code Here
    }
  }

and as such are routed to through ./api/My and ./api/My/?mykey=value

and I've tried setting up my ODataController to follow a similar suit with:

  [ODataRoutePrefix("My")]
  public class oMyController : ODataController {

    [HttpGet]
    public IHttpActionResult Get(ODataQueryOptions<FileModel> queryOptions) {
      //Code Here
    }

    [HttpGet]
    [ODataRoute("({mykey})")]
    public IHttpActionResult Get([FromODataUri] String mykey) {
      //Code Here
    }
  }

defining odata route ahead of time like this:

  ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
  builder.EntitySet<MyModel>("My");
  config.MapODataServiceRoute(
    routeName: "ODataRoute",
    routePrefix: "odata",
    model: builder.GetEdmModel()
  );

but attempts to access ./odata/My and ./odata/My(value) end up going into my APIController instead of the ODataController.

How can I route these using the different prefixes, but the same name, and have them go to the appropriate controllers. I don't want to have a different name for each route if I can prevent it, the prefixes should take care of everything, but for some reason they're not.

Apodal answered 7/4, 2015 at 19:10 Comment(2)
In what namespace ODataRoute is found! I can't find itMalaspina
ODataRoute used to be in the System.Web.Http.OData.Routing namespace, but that has been deprecated. It's new home is under the System.Web.OData.Routing namespace, which you can get as part of the Microsoft.AspNet.OData nuget package.Apodal
A
15

You need to specify the ODataRoute attribute, even if it's empty:

[ODataRoutePrefix("My")]
public class oMyController : ODataController {

  [HttpGet]
  [ODataRoute()] // <---<< This was the key to proper OData routing
  public IHttpActionResult Get(ODataQueryOptions<FileModel> queryOptions) {
    //Code Here
  }

  [HttpGet]
  [ODataRoute("({mykey})")]
  public IHttpActionResult Get([FromODataUri] String mykey) {
    //Code Here
  }

}
Apodal answered 7/4, 2015 at 19:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.