Part - 7 Swagger in ASP.NET Core Web API
Part - 7 Swagger in ASP.NET Core Web API
Let us see how to add the Swagger Packages to our Project. To Add Swagger, open the NuGet
Package Manager window and then search for Swashbuckle.AspNetCore is in the browse tab, as
shown in the image below. Then, choose the Swashbuckle.AspNetCore package, select the project
where you want to install this package, and finally, click on the Install button, as shown in the image
below.
You can customize the Swagger metadata by configuring the SwaggerGen options in Program.cs. So,
modify AddSwaggerGen service as follows. As you can see here, we have provided more information
about our APIs, like, Title, Version, Description, TermsOfService, Contact, License.
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("V10", new OpenApiInfo
{
Title = "My Custom API",
Version = "V10",
Description = "A Brief Description of My APIs",
TermsOfService = new Uri("https://dotnettutorials.net/privacy-policy/"),
Contact = new OpenApiContact
{
Name = "Support",
Email = "[email protected]",
Url = new Uri("https://dotnettutorials.net/contact/")
},
License = new OpenApiLicense
{
Name = "Use Under XYZ",
Url = new Uri("https://dotnettutorials.net/about-us/")
}
});
});
By default, Swagger API looks for the Version V1, but here, we have changed the version to V10. So,
we also need to configure the same into the Request Processing Pipeline. So, next modify the
UseSwaggerUI as follows:
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/V10/swagger.json", "My API V10");
});