Dot net Core Web API Versioning Implementation

 Steps For API Versioning:


  1. Add new NuGet package:

    1. Microsoft.AspNetCore.Mvc.Versioning

    2. Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer


  1. Configure API Versioning Updating by startup. cs file with the following Code:

    1.   services.AddApiVersioning(options =>

            {

                options.DefaultApiVersion = new ApiVersion(1, 0);

                options.AssumeDefaultVersionWhenUnspecified = true;

                options.ReportApiVersions = true;

           });

  1.    services.AddVersionedApiExplorer(options =>

            {

// add the versioned api explorer, which also adds IApiVersionDescriptionProvider service  

// note: the specified format code will format the version as "'v'major[.minor][-status]"  

                options.GroupNameFormat = "'v'VVV";


// note: this option is only necessary when versioning by url segment. the SubstitutionFormat  

// can also be used to control the format of the API version in route templates  

                options.SubstituteApiVersionInUrl = true;

            });


  1. Configure API Versioning With Swagger Updating by StartUp.cs File

    1.             services.AddSwaggerGen(c =>

            {

              

                c.CustomSchemaIds(x => x.FullName);

// configure dropdown for Versioning in swagger page

                c.SwaggerDoc("v1", new OpenApiInfo { Title = "GurooGlobalAPI verison 1", Version = "v1" });

                c.SwaggerDoc("v2", new OpenApiInfo { Title = "GurooGlobalAPI verison 2", Version = "v2" });


     });

  1.   // specifying the Swagger JSON endpoint.

            app.UseSwaggerUI(

            options =>

            {

               

// build a swagger endpoint for each discovered API version  

                foreach (var description in provider.ApiVersionDescriptions)

               {

                    options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());

                }

            });


  1. Configure and Map-Versioning With API Controller and API Action Methods.

    1. Add Route Attribute With Routing string on API Controller

      1.   [Route("v{version:apiVersion}/[controller]")] 

    2. Add Multiple API versions support by that Controller

      1.   [ApiVersion("1")]

      2.    [ApiVersion("2")]

    3. Map API version with Action Method 

      1.    [MapToApiVersion("1")]

Ex. 

    [ApiController]

    [ApiVersion("1")]

    [ApiVersion("2")]

    [Route("v{version:apiVersion}/[controller]")]

    public class CredentialController : ControllerBase

    {

       [HttpPost]

       [Route("GetToken")]

      [MapToApiVersion("1")]

       public async Task<ActionResult<string>> GetToken()

      {

         // api body

       }


     }







Comments

Popular posts from this blog

Dot Net Core Middleware (Authorization + Custom Authorize)

GIT Basic Command & Git Command Flow chart