Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

api version value by default in swagger-ui

I have configure swagger in our asp.core wep-api project and its working perfectly fine.Now i am looking into solution when swagger-ui appears as shown below

https://imgur.com/a/K7QTKCu

the api version part should be fill automatically as per configuration from code side.

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info
            {
                Version = "v1",
                Title = "My API",
                Contact = new Contact
                {
                    Name = "My Api",
                    Url = "https://109.com/"
                }
            });
            var security = new Dictionary<string, IEnumerable<string>>
            {
                {"Bearer", new string[] { }},
            };
            c.AddSecurityDefinition("Bearer", new ApiKeyScheme
            {
                Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                Name = "Authorization",
                In = "header",
                Type = "apiKey"
            });
            c.AddSecurityRequirement(security);
        });
like image 633
Munawar Khan Avatar asked Oct 29 '19 05:10

Munawar Khan


People also ask

How do I find the API version of Swagger UI?

Some distinct identifiers to Swagger UI 3.x: The API version appears as a badge next to its title. If there are schemes or authorizations, they'd appear in a bar above the operations.

What is Swagger documentation for REST API?

Swagger RESTful API Documentation Specification 1 Introduction. Swagger™ is a project used to describe and document RESTful APIs. ... 2 Revision History. Initial release of the formal document. ... 3 Definitions. A resource in Swagger is an entity that has a set of exposed operations. ... 4 Specification. ... 5 Schema. ...

Why am I unable to execute a Swagger UI script?

If you're unable to execute it, you're likely to use an older version, and in that case the first step would be to upgrade. Some distinct identifiers to Swagger UI 3.x: The API version appears at the bottom of the page. Schemes are not rendered.

What is the license for Swagger?

The Swagger specification is licensed under The Apache License, Version 2.0. 1. Introduction Swagger™ is a project used to describe and document RESTful APIs. The Swagger specification defines a set of files required to describe such an API.


Video Answer


2 Answers

You need to install Microsoft.AspNetCore.Mvc.Versioning and Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer packages to enable the API versioning in Swagger.

You can check the additional details at here.

In ConfigureServices method define the versioning scheme as

 services.AddVersionedApiExplorer(o =>
 {
      o.GroupNameFormat = "'v'VVV";
      o.SubstituteApiVersionInUrl = true;
 });
 services.AddApiVersioning(config =>
 {
     config.DefaultApiVersion = new ApiVersion(1, 0);
     config.AssumeDefaultVersionWhenUnspecified = true;
     config.ReportApiVersions = true;
 });
like image 108
user1672994 Avatar answered Oct 20 '22 18:10

user1672994


You need to add options.SubstituteApiVersionInUrl=true to tell swagger to replace the version in the controller route and configure the api version:

services.AddApiVersioning(options => options.ReportApiVersions = true);
services.AddMvcCore()
    .AddJsonFormatters()
    .AddVersionedApiExplorer(
          options =>
          {
              ////The format of the version added to the route URL  
              options.GroupNameFormat = "'v'VVV";
              //Tells swagger to replace the version in the controller route  
              options.SubstituteApiVersionInUrl = true;
          });

Also you need to add this to your controller:

[Route("api/[controller]")]
[ApiVersion("1.0")]
[ApiController]
like image 41
Rena Avatar answered Oct 20 '22 18:10

Rena