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,
irst, create a sample Asp.net core web application.


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


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


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.
public List < Employee > GetEmployee() {
    return 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]"
            }
    };
}


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.

Now click on Get action method, after that click on execute button.

HostForLIFE.eu ASP.NET Core Hosting

European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.