Swagger UI
Nugget Dependancy
Install the Nugget package Swashbuckle.AspNetCore
:
dotnet add Swashbuckle.AspNetCore
Enable XML Documentation Generation
In the .csproj
file of your project, add the following tags as child of the <project>
tag to enable XML comments generation :
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
Configuration
In Startup.ConfigureServices
:
services.AddSwaggerGen(options =>
{
// API Metadata
options.SwaggerDoc("ExampleAppDocumentation", new OpenApiInfo()
{
Title = "Example API",
Description = "Backend for Example App",
Version = "1",
Contact = new OpenApiContact()
{
Email = "john.shepard@n7.al",
Name = "John Shepard",
Url = new Uri("https://arsenelapostolet.fr")
},
License = new OpenApiLicense()
{
Name = "MIT License",
Url = new Uri("https://en.wikipedia.org/wiki/MIT_License")
}
});
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
// Add support for JWT Auth in SwaggerUI
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = "JWT Bearer Authorization",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
},
Scheme = "oauth2",
Name = "Bearer",
In = ParameterLocation.Header
},
new List<string>()
}
});
});
In Startup.Configure
, just after app.UseHttpsRedirection();
:
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/ExampleAppDocumentation/swagger.json", "Example API");
options.RoutePrefix = "";
});
That's it !
No Comments