Versioning Web API actions in ASP.NET MVC 4

前端 未结 5 1162
小蘑菇
小蘑菇 2021-02-01 09:56

I have an ASP.NET MVC 4 app. I want to use the new Web API feature for learning purposes. I want to learn how to expose the same endpoint, but provide different versions of it.

5条回答
  •  北荒
    北荒 (楼主)
    2021-02-01 10:52

    Any chance you still have the default Web API route defined and it's before your custom route? That would cause your scenario to fail. The following route definitions (note the order) worked for me.

    public static void Register(HttpConfiguration config) {
        config.Routes.MapHttpRoute(
            name: "1-0Api",
            routeTemplate: "api/1.0/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
    

提交回复
热议问题