Implementing Swagger in Web API Core 3.1 in clean way

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.

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 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 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.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.


2 thoughts on “Implementing Swagger in Web API Core 3.1 in clean way

  1. Henri January 28, 2020 / 1:37 pm

    Nice ! Next steps would be to implement logging and authentication…

    • Chandradev January 28, 2020 / 3:48 pm

      Thanks Henri. I will post article on that topic.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.