Setup Swagger asp.net core

What is Swagger?

Swagger is an open-source framework which can automatically generate an interactive document for your API in a human and machine-readable way.

Benefits

  1. Save time in creating a spec for your API
  2. The API spec always has the latest changes updated
  3. Makes it easier for QA to test the API

How to Setup Swagger in ASP.NET core 2.0 API

First of all, I need to have an API project to show how we can setup the Swagger; therefore, I am going to create a basic ASP.NET Core API project.

Create an ASP.NET Core API

I am going to create a ASP.NET Core Web Application using Visual Studio 2017, and I will name it TestApi.

Choose Web API template, and make sure you chose the ASP.NET Core 2.0 from the drop-down list.

The API is ready and now we can start setting up the Swagger.

Adding Swagger package to the API Project

  1.  Open Package Manager Console from Tools > NuGet Package Manager > Package Manager Console .
  2.  Type  Install-Package Swashbuckle.AspNetCore , then wait till the package get installed.

Setup Swagger in Startup.cs

Now, you can open the Startup.cs file and setup Swagger there. You can find the changes in lines 29 – 32 and 43 – 48.

Don’t forget to write your application name instead of the word “test” that I used as my app name.

After adding the configurations you can run the project and direct to /swagger where you can see the Swagger generated pages and you can also test against your endpoints.