开发者

MVC3 Restful Versioning Routes

We have a RESTful API that we are doing in MVC3. We would like to have routing versions 开发者_如何学Cto go to different controllers. Our current routing is:

routes.MapRoute("api1", "{controller}/{action}/v1");
//routes.MapRoute("api2", "{controller}/{action}/v2");

In the future, when we make a version 2... what is the best approach to making sure v1 api goes to controller v1 and v2 goes to the version 2 apis?

Thanks.


You can hardcode the controller into the route

routes.MapRoute("api1", "{action}/v1", new { controller = "V1" });
routes.MapRoute("api2", "{action}/v2", new { controller = "V2" });

I would disagree with the other poster that versioning in the URL is necessarily a bad idea. URL versioning is more flexible in terms of output caching.


Versioning, if it is necessary, should not be done via the URL. It should done in the content. That is why you don't see websites creating HTML5 urls for their websites?

The primary objective of REST is it allow clients and servers to evolve independently. In the vast majority of cases versioning should not be required.


An alternative could also be to implement it using ASP.net MVC Areas.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜