Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Swagger Integration in ASP.NET Web API Project

0.00/5 (No votes)
18 Aug 2017 1  
How can I integrate Swagger in ASP.NET Web API project

Introduction

When you create a new ASP.NET Web API project, you need to present your APIs in a simple and comprehensive way? You can use Swagger.

“Swagger is a simple yet powerful representation of your RESTful API. With the largest ecosystem of API tooling on the planet, thousands of developers are supporting Swagger in almost every modern programming language and deployment environment. With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability.”

Background

Able to create ASP.NET Web API 2.0. Use Postman.

Add Swagger to WEB API project

To add Swagger to an ASP.NET Web API, we will install an open-source project called Swashbuckle via NuGet.

When we install the package, a new config file was added: SwaggerConfig as mentioned in the picture below:

Swagger Configuration

In the configuration file added, we find the minimum configuration needed to enable Swagger and Swagger UI:

Now, when we start a new debugging session, we get the URI of our API and we navigate to Swagger: http://localhost:[PORT_NUM]/swagger

And if we want to display our APIs:

So What About Documentation?

If we commented our code, we can enable XML Comments to display it in Swagger.

In Solution Explorer, right-click on the Web API project and click Properties. Click the Build tab and navigate to Output. Make sure XML documentation file is checked. You can leave the default file path. In my case, it is bin\SwaggerSolution.Services.XML.

After we enable the comment in the configuration file of Swagger: SwaggerConfig:

Now, let's run the project and take a look. :)

I added this project as an extension to be able to use it easily in your project.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here