Updated Path Versioning Post Here
Creating a new API with ASP.NET Core is fun and easy; versioning that API is a bit harder. The cinch though is h...
For further actions, you may consider blocking this person and/or reporting abuse
Hi Henrick,
isn't the solution a bit overly complex?
With .Net Core 2.2 I just did:
That is extremely clean and simple. Thanks for showing me this - I was trying to figure out a better way to do this. Using proper namespacing with
ApiExplorerGroupPerVersionConvention
makes a big difference.You're welcome. I also spent some time trying to figure it out and this was the cleanest solution. Enjoy!
Hello slavius, I have this implementation in an API, and without a doubt it is the simplest and cleanest thing there is. I have only identified one problem. the repetition of the endpoint code from version 1 in version 2. I have solved this by declaring my endpoints in an abstract class and inheriting from it in my controller version 1 and version 2. overwriting functionality of any of those at point if necessary and adding new ones. The problem that I keep seeing is that I will not always want all the endpoints of my version 1 to continue in version 2 of my controller. any suggestion for me
Hi argenis96,
this seems to me more as an architectural problem. The controller should only contain domain isolated code required for it to work. All other code should be extracted to appropriate locations (repository, services, shared library) and be re-used in both controllers.
Have a look at the code from identical endpoints in v1 and v2 and try to extract as much as you can outside of the method as functions leaving only the logic. Don't forget to use interfaces for dependencies so you get good testability. Then your code should remain clean from duplicates and easy to maintain and test.
For example, if your controller queries data from a database but v2 of the same controller method uses more fields returned, you can create a function inside your repository to either accept LINQ expression with output fileds or even Where() clause or you can implement method that returns IQueryable() and materialize it inside the controller by defining SELECT columns and calling .ToList() or .ToArray().
I speak too soon, I'm having a problem calling the different versions. How do I decorate my controller to make them unique? I have something like the below but that throw an endpoints.MapControllers() error.
namespace API.Controllers.v1
{
[Route("api/v1/[controller]")]
This was awesome. And you can use whatever you want to mark the versions (attributes, namespaces, class names). I like it.
Thank you, this is clean and nice and work for me in .net core 3.0.
This is a great tutorial. Thanks.
I did get stuck at some point.
With .NET Core 3.1, Swashbuckle has changed a number of things. The "Apply" method in the class that implements the IDocumentFilter should be updated to this:
Also, the predicate that ensures endpoints are displayed in their appropriate swagger doc should be updated:
Saved my day.
Thank you ... this is exactly what I needed!
Hi!!!
I follow allll the steps but i have this Error : "System.ArgumentException: 'An item with the same key has already been added. Key: v1'"
Can u help me???
hi Henrik .thankful for article.
i have problem with Swashbuckle.AspNetCore version 5.0.0-rc4
when i want implement ReplaceVersionWithExactValueInPath class
i get error.
tnx you.