Api Versioning In Asp.net Core Web Api

Author : Sachin Sharma
Published On : 31 Mar 2023

What is API versioning?

API versioning is the practice of managing API changes And ensuring that these changes are made without disrupting clients. we may create multiple APIs that may be consumed by many clients.Businesses grow and expand, resulting in new requirements.Due to this, we may need to provide more functionality in the existing APIs.

However, the existing Web API can be consumed by many clients so how to implement

Is it possible to introduce the new feature without affecting existing customers?

We can solve this problem by versioning our API.

Different ways of Versioning Web API

1) Query string
2) URL 
3) HTTP header 

to implement versioning firstly need to install Package from nuget.

After successfully installation of package,need to make changes in
Startup.cs class.

builder.Services.AddApiVersioning(o =>
{
    o.AssumeDefaultVersionWhenUnspecified = true;
    o.DefaultApiVersion = new Microsoft.AspNetCore.Mvc.ApiVersion(1, 0);
    o.ReportApiVersions = true;
    o.ApiVersionReader = ApiVersionReader.Combine(
        new QueryStringApiVersionReader("api-version"),
        new HeaderApiVersionReader("X-Version"),
        new MediaTypeApiVersionReader("ver"));
}); 

Query String-Based Versioning

to call the specific controller we need to define version in api query string We’ve already set a name for the query string parameter (api-version) that we are going to use to send versioning information.

[ApiController]  
    [ApiVersion("1.0")]  
    [Route("api/employee")]  
    public class EmployeeV1Controller : ControllerBase  
    {  
        [HttpGet]  
        public IActionResult Get()  
        {  
            return new OkObjectResult("employees from v1 controller");  
        }  
    }  


    [ApiController]  
    [ApiVersion("2.0")]  
    [Route("api/employee")]  
    public class EmployeeV2Controller : ControllerBase  
    {  
        [HttpGet]  
        public IActionResult Get()  
        {  
            return new OkObjectResult("employees from v2 controller");  
        }  
    }

To call EmployeeV1Controller, we have to hit as https://localhost:55287/api/employee?api-version=1.0

URL based Versioning

Url versioning is the mostly used and easly readable.

[ApiController]  
    [ApiVersion("1.0")]  
    [Route("api/{v:apiVersion}/employee")]  
    public class EmployeeV1Controller : ControllerBase  
    {  
        [HttpGet]  
        public IActionResult Get()  
        {  
            return new OkObjectResult("employees from v1 controller");  
        }  
    }   

To call EmployeeV1Controller, we have to hit as https://localhost:44381/api/1.0/employee

HTTP Header-Based Versioning

in header based versioning we have to pass version information in Http header. we will use X-Version Key in header because we already defined this key in startup setting.

Http-Header-versioning/

Comments

No comments have been added to this article.

Add Comment