Versioning Web API actions in ASP.NET MVC 4

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

    Sebastiaan Dammann has, on his blog, described how he did Web API versioning by writing his own implementation of IHttpControllerSelector and supporting interfaces.

    http://damsteen.nl/blog/implementing-versioning-in-asp.net-web-api

    He's also put the code on github

    https://github.com/Sebazzz/SDammann.WebApi.Versioning

    And packaged it in NuGet for us! :)

    https://nuget.org/packages/SDammann.WebApi.Versioning

    While implementing IHttpControllerSelector is certainly (imho) the Right Way to do Web API versioning, I think it would be ideal if he included the ability to version based on the HTTP Accept header (see http://barelyenough.org/blog/2008/05/versioning-rest-web-services/).

    Unfortunately my client side is unable to work with the Accept header, so his RouteVersionedControllerSelector is ideal for me.

    Edit: Not sure how I missed it, but there is indeed an AcceptHeaderVersionedControllerSelector that can be used to do versioning the ideal way. I'm currently using it on a new project, but it still has some drawbacks

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-01 10:51

    You're probably running into issues because the controllers have the same name. The controller namespace or the folder it's in doesn't matter at all to WebAPI, only the name does. The simplest thing I can think of is to rename your controllers ProductsV1Controller and ProductsV2Controller and set up two routes to point to your controllers:

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

    Of course, this gets messy if you have multiple controllers you want to expose in this way. Let me see if I can't think of something better for you.

    0 讨论(0)
  • 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 }
        );
    }
    
    0 讨论(0)
  • 2021-02-01 10:54

    Using HttpControllerSelctor Implementation Controller Versioning in Web Api 2 using URL

    for more information you can check and Here

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