Implementing Swagger In Web API

In the previous article, I have given a brief overview of what Swagger is, you can find that article here.
In this article, we will go through how we can implement Swagger with web API.
Testing Web API’s is a challenge. It has a dependency on various third-party tools, requires installing different packages,  and it can get all messed up. Swagger can make it easy and quick.
Let’s go ahead and create a Web API Project,

Implementing Swagger In Web API
Create a project,

Implementing Swagger In Web API
Create an “ASP.NET Web Application.” I have created it with the name- DemoWebAPIWithSwagger.
Now, select WEB API and click on OK,

Implementing Swagger In Web API
Now, our web API Project has been created so let's quickly write a controller,
Go to controllers (right click) > Add > Controllers. 

Implementing Swagger In Web API
Select “Empty Web API Controller”; I have named my controller as loginController.
Writing a controller is not in the scope of this article, so to make it quick I will quickly write a static controller, which accepts a JSON value, and returns either successful or unsuccessful.
  1. using Newtonsoft.Json.Linq;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Net.Http;  
  7. using System.Web.Http;  
  8. namespace DemoWebAPIWithSwagger.Controllers {  
  9.     public class LoginController: ApiController {  
  10.         [HttpPost]  
  11.         [Route("Login")]  
  12.         public JObject loginService([FromBody] JObject loginJson) {  
  13.             JObject retJson = new JObject();  
  14.             string username = loginJson["username"].ToString();  
  15.             string password = loginJson["password"].ToString();  
  16.             if (username == "admin" && password == "admin") {  
  17.                 retJson.Add(new JProperty("authentication ""successful"));  
  18.             } else {  
  19.                 retJson.Add(new JProperty("authentication ""unsuccessful"));  
  20.             }  
  21.             return retJson;  
  22.         }  
  23.     }  
  24. }  
This is a code snippet for the controller, please import Newtonsoft.Json.Linq.
Once the controller had been created, now it's time to add Swagger into our project, for that we need to add a NuGet Package – Swashbuckle.

Implementing Swagger In Web API
Once you have installed Swashbuckle to your project you can find a SwaggerConfig file in App_Start.

Implementing Swagger In Web API
By default Swagger does not show XML Comments, there is an option to display them with Swagger UI, but for that, we need to make sure, when the project is built all the XML comments get saved into the XML file. For that, first, go to project properties. In the Build Section check > XML documentation file, you will then get a path. Mine is bin\DemoWebAPIWithSwagger.XML
Save it with Ctrl + S,
Now, go to SwaggerConfig file shown above, here search for c.IncludeXmlComments(GetXmlCommentsPath()) and uncomment it. Now
you can generate the method using intellisense or you can write a method on the end of the config file.
  1. private static string GetXmlCommentsPath()  
  2. {  
  3.    return System.AppDomain.CurrentDomain.BaseDirectory + @"\bin\DemoWebAPIWithSwagger.XML";  
  4. }   
Change the type of method to string.
We are all done now... Let’s run our project. 

Implementing Swagger In Web API
Add /Swagger in url = > http://localhost:5543/swagger
You can find our Swagger UI Here,

Implementing Swagger In Web API
Testing it.

Implementing Swagger In Web API
You can find the code attached.