How To Configure Swagger UI In ASP.NET Core Web API

Introduction 

Swagger is an open-source tool that is used to interact directly with the API through the Swagger UI. In this article, we will see how we can add Swagger in ASP.Net Core application and generate documentation for our web API.

Let's get started,

First, create a sample Asp.net core web application.

How to configure Swagger UI in Aspnet core web Api

Give a meaningful name to the application, here I have given the name as SwaggerDemoApplication.

How to configure Swagger UI in Aspnet core web Api

Once the project is created let's add a new controller. I have added HomeController. Once the Controller is created add the following NuGet packages into the application,

Swashbuckle.AspNetCore
Swashbuckle.AspNetCore.Swagger
Swashbuckle.AspNetCore.SwaggerUI

How to configure Swagger UI in Aspnet core web Api

Create new folder Model and add Employee class.

public class Employee {
    public int Id {
        get;
        set;
    }
    public string FirstName {
        get;
        set;
    }
    public string LastName {
        get;
        set;
    }
    public string EmailId {
        get;
        set;
    }
}

Now add below Actionmethod into Homecontroller.

[HttpGet("api/home")]
public ActionResult<IEnumerable<Employee>> GetEmployees()
{
    var employees = new List<Employee>()
    {
        new Employee()
        {
            Id = 1,
            FirstName = "Yogesh",
            LastName = "Vedpathak",
            EmailId = "[email protected]"
        },
        new Employee()
        {
            Id = 2,
            FirstName = "Amit",
            LastName = "Kanse",
            EmailId = "[email protected]"
        }
    };

    return Ok(employees); // Return a 200 OK response with the list of employees
}

Configuring the Swagger Middleware

Now it's time to configure services inside a startup.cs class. Open startup.cs class and add the below line of code into configuring services method.

public void ConfigureServices(IServiceCollection services) {
    // Register the Swagger or more Swagger documents
    services.AddSwaggerGen(c => {
        c.SwaggerDoc("v1", new OpenApiInfo {
            Title = "SwaggerDemoApplication", Version = "v1"
        });
    });
    services.AddControllers();
}

Add the below line of code inside configure method. Basically we are going to enable middleware for swagger UI.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
        // Enable middleware Swagger for JSON endpoint.
        app.UseSwagger();
        app.UseSwaggerUI(c => {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", SwaggerDemoApplication V1 ");
                });
        }

Now it's time to check the result. Run the application and navigate to https://localhost:44338/swagger/Index.html.

How to configure Swagger UI in Aspnet core web Api

Now click on Get action method, after that click on execute button and see the result.

How to configure Swagger UI in Aspnet core web Api

Summary

In this article, we have seen how to configure Swagger Ui in the ASP.Net core web Application 

Hope you like it.

Happy Coding.