Add Swagger UI in Asp.Net CORE API 3.1

Tech Requirements:

Visual Studio 2019 Community Edition
Asp.Net Core 3.1 SDK
Nuget Packages:
Swashbuckle.AspNetCore.Swagger
Swashbuckle.AspNetCore.SwaggerUI
Swashbuckle.AspNetCore.SwaggerGen

Steps to implementation:

Create a project for .Net Core API
Add below Nuget Packages using Nuget Package Manager Console
Swashbuckle.AspNetCore.Swagger
Swashbuckle.AspNetCore.SwaggerUI
Swashbuckle.AspNetCore.SwaggerGen

Code Snippet:

 Configure Swagger in Project

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
//Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint(“/swagger/v1/swagger.json”, “My API V1”);
});

app.UseHttpsRedirection();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}

Configure Swagger Dependency Service

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc(“v1”, new OpenApiInfo
{
Title = “Employee API”,
Version = “v1”,
Description = “An API to perform Employee operations”,
TermsOfService = new Uri(“https://example.com/terms”),
Contact = new OpenApiContact
{
Name = “Tes User”,
Email = “dnh@gmail.com”,
Url = new Uri(“https://twitter.com/rupakrajput5”),
},
License = new OpenApiLicense
{
Name = “Employee API LICX”,
Url = new Uri(“https://example.com/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);
c.IncludeXmlComments(xmlPath);
});
services.AddControllers();
}

Swagger Output: