Web Api multiple get with the same signature routing

I am creating a web api that has multiple get / post calls that have the same signature. Now I know that in the case of several identical calls, you usually have 2 options: separate for different controllers or using {action} in your routes. I moved on to the {action} method, as it works best for most of my controllers. However, in one of my controllers, I would prefer not to use an action method.

I have a call like this:

[HttpGet] public Program Program(string venue, string eventId) //api/{controller}/{venue}/{eventId} 

Now i need a new challenge

 [HttpGet] public Program ProgramStartTime(string venue, string eventId) //api/{controller}/{venue}/{eventId} 

I know that I can add an action name to it and call ie

 api/{controller}/{action}/{venue}/{eventId} 

But I feel that this is breaking the expected. Is there a way that I could be something like

 api/Content/LAA/1/PST api/Content/LAA/1?PST 

Also, if I need to go the route of actions, I already have a route that I use for other controllers, but it just uses {id} as the only parameter. Will the new route contradict this? Is there a better way to configure my routes?

 config.Routes.MapHttpRoute( name: "...", routeTemplate: "api/{controller}/{action}/{id}", defaults: new {id = RouteParameter.Optional} ); config.Routes.MapHttpRoute( name: "...", routeTemplate: "api/{controller}/{action}/{venue}/{eventId}/{...}/{***}/{###}", defaults: new {### = RouteParameter.Optional} ); config.Routes.MapHttpRoute( name: "...", routeTemplate: "api/{controller}/{action}/{venue}/{eventId}/{...}", defaults: new {... = RouteParameter.Optional} ); config.Routes.MapHttpRoute( name: "...", routeTemplate: "api/{controller}/{action}/{venue}", defaults: new {venue = RouteParameter.Optional} ); 

I expect at least one method that would have up to 5 parameters

+3
source share
1 answer

Here is the answer I found, and it does pretty much what I wanted:

  config.Routes.MapHttpRoute( name: "VenuesAllOrStream", routeTemplate: "api/Racing/{action}", defaults: new { controller = "Racing", action = "Venues" }, constraints: new { action = "Venues|All|Streaming" } ); config.Routes.MapHttpRoute( name: "VenueOrVideo", routeTemplate: "api/Racing/{venue}/{action}", defaults: new { controller = "Racing", action = "RaceNumbers" }, constraints: new { action = "RaceNumbers|Video" } ); config.Routes.MapHttpRoute( name: "ProgramOrMtp", routeTemplate: "api/Racing/{venue}/{race}/{action}", defaults: new { controller = "Racing", action = "Program" }, constraints: new { action = "Program|Mtp", race = @"\d+" } ); 

It is important that VenuesAllOrStream be the first, since otherwise VenueOrVideo takes the route. I will most likely choose action restrictions in the transfers later.

Overview: setting a default action allows a route to basically make it an optional parameter. Thus, each route works without the actual set of {action}.

+3
source

Source: https://habr.com/ru/post/1207022/


All Articles