Click here to Skip to main content
15,886,008 members
Articles / .NET / .NET4.6
Tip/Trick

Swagger Integration in ASP.NET Web API Project

Rate me:
Please Sign up or sign in to vote.
4.82/5 (5 votes)
18 Aug 2017CPOL1 min read 19.7K   5  
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.

Image 1

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

Image 2

Swagger Configuration

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

Image 3

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:

Image 4

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:

Image 5

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.

Image 6

Image 7

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
Canada Canada
I am Rebaï Hamida, Microsoft MVP in Visual Studio and Development Technologies.
I'm a software architect and developer who like to build open source projects, writing articles, learning and teaching best practices. you can find out in my blog : http://hamidarebai.blogspot.com/

Innovative Software engineer : hands-on, competent software engineer, passionate and proficient C# developer, offering more than seven years of experience in the full software development lifecycle – from concept through delivery of next-generation applications and customized solutions, producing code to a consistently high standard and testing software across a variety of platforms.
Expert in advanced development methodologies, tools and processes contributing to the design and roll-out of cutting-edge software applications.
Known for excellent troubleshooting skills – able to analyze code and engineer well-researched, cost-effective and responsive solutions.


Success is only a consequence of a continuous work and remarkable intelligence.
I believe that I have to make a change in my country, I can’t live without leaving my footprint.

Comments and Discussions

 
-- There are no messages in this forum --