Thursday, 12 October 2017

Web API: Swagger Integration in ASP.NET Web API Project

This article is about to configure the swagger ui in the Web API to test the API end points. Following are very simple steps to install and configure the swagger in your web API project.


Here I am assuming that you have already added the Web API project. So after adding the web API project follow the following steps to install swagger in the Web API:


Step 1: Install nuget package named "SwashBuckle" in your Web API project. Please check the following screenshot.




Step 2: Right click on the Web API project. 
Go to Properties => Build => Output => Check the "XML documentation file:" option.




Step 3: Now open App_Start => SwaggerConfig.cs file and make some modifications to enable xml documentation as following:




Add the following function:
protected static string GetXmlCommentsPath()
        {
            return System.String.Format(@"{0}\bin\SwaggerAPISample.XML",
                System.AppDomain.CurrentDomain.BaseDirectory);
        }

Uncomment the following line in Register function:
 c.IncludeXmlComments(GetXmlCommentsPath());

Step 4: Now start adding xml comments on the API methods as following screenshot:



Step 5: Run the Web API project by pressing "F5" and browse the url as following in the browser:
http://localhost:49460/swagger/ui/index



Download the sample code here.

No comments:

Post a Comment