Configuring and Implementing swagger in ASP.Net Core web API

Ahmed Thaajwer
3 min readOct 21, 2020

Swagger is an API documentation framework. It is an implementation of Open API specifications. It is widely used by the development community for describing and defining API documentation. Here I am going to explain how to implement an ASP.Net core application using Swashbuckle.AspNetCore NuGet package.

first, I am going to create a new web application project in asp.net core

Then I am going to name it as swagger.test and create as a web API.

Step-01

After creating a new project, right-click on the project and go to the manage Nuget packages and select the browse option. it will show all packages available for ASP.Net core.

then, search the Swashbuckle.AspNetcore and install it.

Step-02

After installing the Swashbuckle.AspNetcore package, I open the startup.cs file and update. here I am going to add the necessary configurations to set up the swagger.

First, I add the swagger generator to the dependency injection container. for that, I use the service collection objects as services.AddSwaggerGen();

Step-03

Next, I am going to configure method in the startup.cs file. In the Startup.Configure method, I add the app.UseSwagger(); to add the swagger middleware to the asp.net core and add the app.UseSwaggerUI(); to specifying the Swagger JSON endpoint.

Step-04

After we save the startup.cs, we can able to run the application and we can able to see the swagger documentation. we get the output of default WeatherForecast API. Then change the path of weatherforecast documentation to swagger documentation by changing the path of the URL from https://localhost:44348/weatherforecast to https://localhost:44348/swagger to show the swagger documentation UI.

That's it. Now we can able to explore the API via the Swagger UI and it will be easier to incorporate it into other applications. We can see each controller and its API action methods listed here. Once we click on an action method, detailed information will be visualised like parameters, response, and example values. There is also an option to try out each of those action methods:

By clicking on the Try it out button, we can test the endpoint and see the response:

That's all here. For more information about Swashbuckle and ASP.NET Core, refer this link below.

https://docs.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-3.1&tabs=netcore-cli

--

--

Ahmed Thaajwer

B.Sc.(hons) in Computing & Information System(SUSL). Former Software Engineer@D6 Digital. IEEE Brand Ambassador@IEEE. Regional Country Director@UYG.