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);
});
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.
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. ...
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.
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.
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;
});
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With