DEV Community

REST API versioning with ASP.NET Core

99darshan on January 16, 2020

The best way to version a RESTful API is a topic of constant debate and every other approach have their own pros and cons. There is no 'one size fi...
Collapse
 
dishanx profile image
Dishan

Good post.
Is it possible to use URL path versioning and header API versioning combination? I added URL versioning support to my API but it breaks access to the old URL(api/authors). now I have to specify version in the URL(api/v1.0/authors). but some of the client apps use that old URL. are there any solutions for that?

Collapse
 
99darshan profile image
99darshan

Yes you could use multiple versioning schemes. Supporting multiple versioning Schemes section in this article has an example on how to implement both the query params versioning scheme and request header versioning scheme.

Collapse
 
prayatna profile image
Prayatna Bhattarai

You can try adding two url paths like
[Route("api/[controller]")]
[Route("api/v{version:apiVersion}/[controller]")]
for your base controller so that it uses both url

Collapse
 
apis3445 profile image
apis3445

And how do you work with changes on db? for example v1 has one table with 5 fields, v2 you split one field into 2 fields, how can you automate the database changes when an user wants to use the new versiΓ³n?

Collapse
 
kirbypaint profile image
Ash Porter

This was an excellent post, thank you so much for taking the time to put this together!

I wanted to say, on the line where it specifies the API version key and value:
demo.org/api/resource?v=1.1

Having it only be "v=1.1" didn't work for me. I actually had to match that to the image provided directly below, with that parameter being "api-version=1.1"

Once I did that, it worked perfectly!

Collapse
 
99darshan profile image
99darshan

Thank you for spotting this. The one on the screenshot is correct. I'll update the article.

Collapse
 
hdsoftware profile image
HD Software

Question about versioning the datamodel
Lets say you have an API that use AutoMapper on a datamodel.
Now, if the datamodel changes, the API will still compile like nothing happend, but the end result wil not be compatible anymore

I see two different ways out of this:

1:
Project
- DTO
- V1
PersonDTO
- V2
PersonDTO
2:
Project
- DTO
PersonDTO_V1
PersonDTO_V2

What would be considered best practice here?

Collapse
 
hdsoftware profile image
HD Software

Just figured it out myself by testing
Adding version to the ClassName is best because else having USING statements to support multiple versions in the same API class wil create problems if ClassNames are the same

Collapse
 
delossantosam profile image
delossantosam

That is a great post, in your opinion, what are the best approaches for versioning a REST API?

Collapse
 
99darshan profile image
99darshan

I think it depends on the nature and complexity of the REST API. In the articles, I've laid out the few approaches. For simpler APIs I'd go with either using path param approach or using the request headers.

Collapse
 
delossantosam profile image
delossantosam

Thanks for the answer :)

Collapse
 
brian611 profile image
Brian Pooe

Great post. I am new to this i am wondering how to do you cater for DTO changes when versioning? Do I duplicate them and cut or add fields needed for a particular version or I use one DTO?

Collapse
 
ghodgey profile image
Gareth Hodgson

Great article!
Thanks so much for this!

Collapse
 
devingoble profile image
Devin Goble

This is great. It's so nice to see these options baked in instead of having to roll our own support. Thanks for writing this up!

Collapse
 
haripraghashs profile image
Hari subramaniam

Thanks for sharing this. Never knew there was a version management library within asp net core

Collapse
 
alainkaiser profile image
Alain Kaiser

Great post, thank you!

Collapse
 
imabhishektomar profile image
Abhishek Tomar

Good post.

Collapse
 
shojajou profile image
Mojtaba Shojajou

Thanks for your good guide. There's a question here. Can we version API data models?