Versioning Web API actions in ASP.NET MVC 4

前端 未结 5 1163
小蘑菇
小蘑菇 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:44

    If we are going your 1st approach then it helps to route and allow us to get the data for V1, V2.... but now we have taken an example of v1 and v2 for one controller so routing code will be as below:

    config.Routes.MapHttpRoute(
        name: "1-0Api",
        routeTemplate: "tables/v1/Products",
        defaults: new { controller = "ProductsV1", id = RouteParameter.Optional }
    );
    config.Routes.MapHttpRoute(
        name: "2-0Api",
        routeTemplate: "tables/v2/Products",
        defaults: new { controller = "ProductsV2", id = RouteParameter.Optional }
    );
    

    But we have more than 20 controllers and multiple versions then how to make it generic.

提交回复
热议问题