OData v4 routing prefix?

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

[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 sent through. / api / my and. / api / my /? mykey = value

and I tried to configure my ODataController to execute a similar option:

  [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 } } 

odata route determination ahead of time:

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

but trying to access. / odata / My and. / odata / My (value) end up in my APIC controller instead of the ODataController.

How can I send them using different prefixes, but with the same name, and ask them to go to the corresponding controllers. I do not want to have a different name for each route, if I can prevent it, prefixes should take care of everything, but for some reason this is not so.

+9
c # odata asp.net-web-api asp.net-web-api-routing odata-v4
source share
2 answers

Wow, I feel a little stupid. 20 minutes after posting, I solve it. In short, I needed to specify the ODataRoute attribute, even if it is empty, so my new ODataController, which works, looks like this:

 [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 } } 
+13
source share

I could not get ODataRoutePrefix to work with ODataConventionModelBuilder.

Can you please your Startup.cs file? In particular, where the EntitySet is configured

0
source share

All Articles