Introduction
There are many challenges we face for consuming a Web API because it contains various methods such as - GET, POST, PUT, DELETE. All these methods contain various types of parameters like model, string, int, etc. We don’t know what exact properties we need to pass in the model parameter and what are the relevant ones. These are the major challenges for a developer and so we need proper documentation to solve this problem. That’s why we choose Swagger, also known as OpenAPI. It provides all such benefits like interactive documentation, client SDK generation, and API discoverability. In this article, I am explaining a few basic configurations of Swagger in ASP.NET Core applications. We can add more additional features on the Web API using Swagger. For that, just read the reference document that I have mentioned in the Reference section.
Before reading this article, you must read the articles given below for ASP.NET Core knowledge.
We have tested the Swagger documentation application in the latest VS 2019. So, please check the following steps to kickstart the initial process of installation.
Open Visual Studio 2019 and click on "Create a new project".

Click on ASP.NET Core Web Application.

Provision your new project and give the appropriate name and the location to be saved.

Choose API and click on the "Create" button on the right side.

Open "Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution…" and click the Browse tab. Search for "Swashbuckle.AspNetCore" in the search bar and install it.

Model
We are going to create an Employee model for demo purposes.
- namespace SwaggerDocumentation.Model
- {
- public class Employee
- {
- /// <summary>
- /// Employee id
- /// </summary>
- public int id { get; set; }
- /// <summary>
- /// Name of the employee.
- /// </summary>
- public string Name { get; set; }
- /// <summary>
- /// Employee personal address.
- /// </summary>
- public string Adress { get; set; }
- /// <summary>
- /// Department unit of the employee.
- /// </summary>
- public string Department { get; set; }
- }
- }
API Version Separation
In future, if we are planning to release multiple versions of an API, then, for better readability we can create a version folder for the API creation. This will help us to differentiate multiple versions in the API side and Swagger documentation. In the following screenshot, we have created two folders - one is "v1" ( Version 1 ) and another one is "v2" ( Version 2 ). Obviously, v2 will contain the latest version comparing to v1.

API Controller
We have created "EmployeeController" as the API controller in our application. Here we can see at the route level the API path is set as "api/v1/[controller]" because when you hit Swagger it will first check the controller level then it will take two identical controller names as “Employee”. This will create an ambiguous issue in the HTTP request controller level, for that reason we have added two different request paths for both versions, v1 & v2.


James ParkerPosted Sep 15, 2019, 2:04 PM
I had to add the following code in Sartup.cs --> ConfigureServices to get this to work: services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "NetChex API", Version = "v1" }); c.SwaggerDoc("v2", new Info { Title = "NetChex API", Version = "v2" }); }); In addition, the Swagger url that works for me is: https://localhost:44372/swagger/ (not https://localhost:44372/api/swagger/) Please advise
Denis OsipenkoPosted Jun 29, 2019, 11:50 AM
Thank you so much!
Jignesh KumarPosted Jun 26, 2019, 8:51 PM
It's informative, thanks for sharing
Amit MohantyPosted Jun 26, 2019, 2:15 AM
Nice article !!!