API Versioning in ASP.NET Core

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.

API Versioning in ASP.NET Core 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.

API Versioning in ASP.NET Core

4/ How to call it via Postman By adding api-version in query string parameter we can define our version API Versioning in ASP.NET Core 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. API Versioning in ASP.NET Core That’s all for this Saturday, Hakuna Matata :)

This article was originally published at https://mwaseemzakir.substack.com/ on .

Whenever you're ready, there are 3 ways I can help you:

  1. Subscribe to my youtube channel : For in-depth tutorials, coding tips, and industry insights.
  2. Promote yourself to 9,000+ subscribers : By sponsoring this newsletter
  3. Patreon community : Get access to all of my blogs and articles at one place