What is the Swagger ?
This is one of the best tool for testing web api method. It will also give very good user friendly definition of web api method.
Advantages:
1. We can easily test web api method
2. We will get user friendly documented message about web api method
3. We can easily debug the web api method using this tool.
How to implement in Asp.net Web API Core 3.1 in Clean way ?
Step 1: Firstly create the web api 3.1 project like this
Creating Web Api Core 3.0 layer using Dapper and .net standard 2.0
Step 2: Go to Web API layer and install the swagger related nuget package

Note: if you are using asp.net core 3.0 or 3.1 then install the swagger version v5.0
Step 3: Create SwaggerExtension.cs file in the Extensions folder of We API Layer and write the Extension Method for IServiceCollection and IApplicationBuilder like this
using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.OpenApi.Models; namespace Dapper_Demo.Extensions { public static class SwaggerExtension { public static void AddSwagger(this IServiceCollection services) { // Register the Swagger generator, defining 1 or more Swagger documents services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "EmpManagement API", Version = "v1" }); }); } public static void AddCustomSwagger(this IApplicationBuilder app) { //Adding Swagger and SwaggerUI app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "EmpManagement ASP.NET Core API v1"); }); } } }
Step 4: Go to the Startup file of application and add the swagger in middleware and service configure like this

Step 5: Run the application and type this URL
If you want to download the source code for this demo. You can download from below demo.
https://github.com/Chandradev819/Dapper_Demo_With_Asp.netCore