3

We have a RESTful API that we are doing in MVC3. We would like to have routing versions to 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.

Sean
  • 2,496
  • 7
  • 32
  • 60

3 Answers3

3

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.

Dmitry S.
  • 8,373
  • 2
  • 39
  • 49
  • Just make sure you know you aren't implementing REST at this point, just following a half-understood fad. Darrel Miller below answered best. – one.beat.consumer Jan 04 '12 at 18:29
  • I agree that in real REST services URL versioning could break hypermedia links in a resource. It would make sense to define custom content types for that. The answer was intended to solve the original question instead of simply focusing on REST definition. – Dmitry S. Jan 05 '12 at 04:45
  • new { controller = "V1" }... It expects by default names with "Controller"-part – Regfor Jan 17 '14 at 15:50
0

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

Juri
  • 32,424
  • 20
  • 102
  • 136
0

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.

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • REST versioning via URL is not only good practice..it's a REST Standard used by most big web players (Facebook, twitter,etc..) – Kevin Jensen Nov 03 '11 at 19:26
  • @KevinJensen If you spend a bit more time looking you will discover that most of the big web players use REST as a buzzword and ignore the constraints of REST. Also if you talk to any experienced REST developer they will tell you the same as I am saying. – Darrel Miller Nov 03 '11 at 20:04
  • While I agree that term 'REST' is often thrown around, and GET/POST/PUT/DELETE are often under-utilized..still doesn't validate your theory. I also understand that url versioning violates REST theory..but it's a much more pragmatic approach and thats why it has such a large following. Have a look here: http://blog.apigee.com/taglist/Versioning – Kevin Jensen Nov 03 '11 at 20:30
  • 1
    @KevinJensen It's not my theory. http://barelyenough.org/blog/2008/05/versioning-rest-web-services/ http://stackoverflow.com/questions/972226/how-to-version-rest-uris/975394#975394 and if you are want to know the impact of "pragmatic" REST, see this http://nick.typepad.com/blog/2011/11/the-long-term-failure-of-web-apis.html – Darrel Miller Nov 03 '11 at 20:41