Add Swagger to ASP.NET Core 2.1 Web API

salman lone
3 min readJun 14, 2019

--

Testing an API is a challenge and it depends of various third party tools to test an API. But swagger make the life easy for testing an API. Swagger is a UI representation of your REST API. It allows anyone, either from development team or from client who is consuming your API, to test, visualize and interact with the API’s resources.

Swagger gives an auto generated documentation of you API resources by reading the .xml documentation file.

In this post, we will see how we can add swagger to ASP .NET Core 2.1 Web API. We will use the default Web API project created by Visual Studio 2017 which contains ValuesController with API actions for all HTTP verbs.

Add Swagger to ASP.NET Core 2.1 Web API

Create an ASP .NET Core 2.0 Web API application using Visual Studio 2017. After creating an application, go to Manage Nuget Packages and add Swashbuckle.AspNetCore nuget package in your project.

Open Startup.cs file to add swagger service to middleware. Like:

and enable the Swagger UI in Configure() method.

This is all required to set up Swagger in you project. Now if you want to open Swagger UI as your home page. Right click on your project and go to Properties. In Debug tab, you can see a check box option for “Launch browser”. Change the value in a text box with “swagger”.

Run the application and you should see Swagger UI for ValuesController.

This is achieved with the minimum settings and without any customization for Swagger.

Swagger can be customized to include method’s XML comments, showing Enum values as string values and generating different swagger document for different API version. We are keeping this article simple to get a start for integration of Swagger in ASP .NET Core Web API 2.1

For customized settings, we have another article posted.

Keep reading!

--

--