Swagger - NeoSOFT-Technologies/rest-dot-net-core GitHub Wiki
Swagger UI allows anyone — be it your development team or your end consumers — to visualize and interact with the API’s resources without having any of the implementation logic in place. It’s automatically generated from your OpenAPI (formerly known as Swagger) Specification, with the visual documentation making it easy for back-end implementation and client side consumption.
Swagger UI works in the latest versions of Chrome, Safari, Firefox, and Edge.
We need to install Swashbuckle.AspNetCore and Swashbuckle.AspNetCore.Swagger packages from NuGet Package Manager.
We need to write extension service class code for AddSwaggerExtension as below,
public static void AddSwaggerExtension(this IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = @"JWT Authorization header using the Bearer scheme. \r\n\r\n
Enter 'Bearer' [space] and then your token in the text input below.
\r\n\r\nExample: 'Bearer 12345abcdef'",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
},
Scheme = "oauth2",
Name = "Bearer",
In = ParameterLocation.Header,
},
new List<string>()
}
});
c.OperationFilter<FileResultContentTypeOperationFilter>();
});
}
Now we need to configure AddSwaggerExtension and AddSwaggerGen services in Startup.cs class. Please refer to the below snippet which is required to set up Swagger UI.
Swagger UI Page will look like as below,
Demo video to get the clear result view of above implemented module.