2015-12-10 18 views

Antwort

15
public static class WebApiConfig 
    { 
     public static void Register(HttpConfiguration config) 
     { 
      // Web API configuration and services 
      // Configure Web API to use only bearer token authentication. 
      config.SuppressDefaultHostAuthentication(); 
      config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); 

      // Web API routes 
      config.MapHttpAttributeRoutes(); 

      config.Routes.MapHttpRoute(
       name: "route1", 
       routeTemplate: "calculate", 
       defaults: new { controller = "Calculator", action = "Get" }); 

      config.Routes.MapHttpRoute(
       name: "route2", 
       routeTemplate: "v2/calculate", 
       defaults: new { controller = "Calculator", action = "Get" }); 

      config.Routes.MapHttpRoute(
       name: "DefaultApi", 
       routeTemplate: "api/{controller}/{id}", 
       defaults: new { id = RouteParameter.Optional } 
      ); 
     } 
    } 

ODER

public class CalculatorController: ApiController 
{ 
    [Route("calculate")] 
    [Route("v2/calculate")] 
    public String Get() { 
     return "xxxx"; 
    } 
} 
+0

Dank @yyou. Was, wenn ich das auf Controller-Ebene machen wollte? Zum Beispiel das Hinzufügen von 2 [RoutePrefix] zur Controller-Klasse – Darren

+0

@Darren Ich habe die Antwort aktualisiert. – yyou

+0

danke! bemerkte, dass es keine Möglichkeit gibt, mehr als einen [RoutePrefix] zu haben. Eine weitere Problemumgehung finden Sie unter http://stackoverflow.com/questions/24953660/asp-net-web-api-multiple-routeprefix. Deine erste Lösung funktioniert für meine Situation. :) – Darren

Verwandte Themen