Web Api multiple get with same signature routing

后端 未结 1 1364
南方客
南方客 2021-01-14 18:09

I\'m building a web api that has multiple get/post calls that have the same signatures. Now I know that in the case of multiple identical calls, you generally have 2 options

相关标签:
1条回答
  • 2021-01-14 18:35

    Here's the answer I found and it does pretty much exactly 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 the VenuesAllOrStream is first as otherwise the VenueOrVideo picks up the route. I most likely will extract out the action constraints into enums later.

    Brief note : Setting the action default allows for the route to basically make it an optional parameter. So each route works without the {action} actually being set.

    0 讨论(0)
提交回复
热议问题