Versioning Web API actions in ASP.NET MVC 4

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

提交回复
热议问题