Integrate Swagger with Web API 2.0 using Swashbuckle


Have you ever wondered how to share Web API with others. You have to share a detailed document specifying all the urls, parameters etc.

Then also they will call you to know exactly how to use the web APIs. They will use tools like  Postman or Fiddler to test the Web API. They have to download the tool and learn how to use the respective tool. Get used to that tool and its interface.

With Swagger you will be able to share your Web APIs to your customers along with metadata . This metadata will be used to test the APIs.

Let us create a simple Web API and integrate Swagger with it. This is a simple example with uses all the default setting. I will share advance options in the coming posts.

Step 1. Create a new Web API 2.0 application using Visual studio . Go to File -> New -> Project and name the project as Swagger-WebApi. | Swagger

Step 2. Choose Web API as project type and change authentication type to no authentication as show below.

Swagger | API 2.0

Step 3. Click OK and a new Web API  project will be created. Run the project in visual studio. Go to the URL http://localhost:59537/Help the port 59537 will be different in your case.Swagger | Api Help


Step 4. To integrate swagger in the Web API project we will use a Nuget package Swashbuckle. Swashbuckle is the dot net implementation of Swagger.Go to Tools -> Nuget Package Manager -> Manage Nuget Package for solution and search for Swashbuckle as shown below

Swagger | Swashbuckle

Step 5.Click install above and click OK to the confirmation dialog box. In the App_Start folder a new  file named SwaggerConfig.cs will be created. This file contains all the configuration needed for the swagger to generate Web API metadata. As shown below.

Swagger | WebApi2 |Swashbuckle

Step 6. Run the application and  go to the url http://localhost:59537/swagger the port number 59537 will be different in your case.

This will open the Web API swagger UI. By default only the name of the Web API will be shown. To see the complete meta data click on the API name, in this case “Values”. This will expand all the Web API urls with option to try the APIs.

Step 7. To try the APIs click on the API urls and it will expand showing all options.

Swagger | Demo

Step 8. You can download the complete project from here download

Be the first to comment

Leave a Reply

Your email address will not be published.