This week's newsletter is sponsored by Milan Jovanovic
Pragmatic Clean Architecture: Learn how to confidently ship well-architected production-ready apps using clean architecture.
Milan used exact principles to build large-scale systems in the past 6 years.
In today’s newsletter we are going to discuss:
What is API Versioning
Why we need it
Implementation in .NET 6
What is API Versioning
API versioning is a technique that allows you to make changes and enhancements to your API without breaking existing client applications.
Why we need it ?
It gives us three main benefits and those are the reasons why need it:
Backward Compatibility
Different versions of your API can coexist, ensuring that older clients continue to function as expected.
Maintainability
As your API evolves over time, versioning helps you keep track of changes and manage different iterations of the API.
Granular Control:
API versioning provides granular control over which version of an endpoint a client application can access. This enables you to selectively expose new features to specific clients while ensuring that others continue to use older versions of the API.
Enough theory let’s dive into action !!!!
Implementation of API Versioning
It consists on three simple steps:
1/ Install Nuget Package
Microsoft.AspNetCore.Mvc.Versioning
2/ Configure Program.cs
builder.services.AddApiVersioning
is enough to enable versioning.
Then we can set options of service according our needs.
First two options set the default version if user does not specify and second line makes sure which version is default.
ReportApiVersion
tells us that which versions are supported to client, to enable it we need to set its value to true.
Note : It is good to read these values from appsetting instead of hard coding.
3/ Add versions on controllers
Tell controller that we are going to enable versioning here by using ApiVersion
attribute.
Then by using MaToApiVersion
specify that for which versions this method/endpoint is available.
4/ How to call it via Postman
By adding api-version in query string parameter we can define our version
By default api version is read from query string parameter but we can add multiple options or even single as well. For multiple options add these lines of code in your service.
That’s all for this Saturday, Hakuna Matata :)
Whenever you’re ready, there are 3 ways I can help you
Promote yourself to 8000+ subscribers by Sponsoring my Newsletter
Become a Patron and get access to 140+ .NET Questions and Answers
Get a FREE eBook from Gumroad that contains 30 .NET Tips (3K downloads)
Subscribe my YouTube Channel where I would be publishing .NET stuff soon
Special Offers 📢 [ 10% Discount]
Ultimate ASP.NET Core Web API Second Edition - Premium Package
Use this promo code :- 9s6nuez to claim discount