preface

Swagger can automatically generate interface documents according to the interface code in the Controller, and can be debugged online, very convenient.

1. Create a.net Web Api projectAlready available to skip

1.1 Open VS >> Create a new project >> Select ASP.NET Web Application (.net Frmework) >> Next

1.2 Entering a project name >> Select a project storage address >> Create

1.3 In the lower right corner, select HTTPS configuration, deselect Create, and wait until the creation is complete

2. Install Swashbuckle and Swagger.Net.UI NuGet packages

2.1 Right-click project >> Select Manage NuGet package

2.2 choose view > > input Swashbuckle/Swagger.Net.UI (two NuGet package) > > choose name exactly match the NuGet package > > installation > > > wait a few minutes > saw green download arrow NuGet package under the installation is successful

2.3 You should see swaggerConfig. cs and Swaggernet. cs files in the App_Start folder

3. Set parameters

3.1 Selecting a project, press Alt+Enter to open the project properties window, or right-click the project and select properties

3.2 Select Generate >> Fill in the value after canceling the warning parameter: 1591 >> Check the XML document file options >> Ctrl+S save the Settings

4. Configuration Swagger

4.1 Enter swaggerNet. cs file >> Ctrl+K+C comment code as shown in figure >> Save

4.2 Enter swaggerConfig. cs file >> Add the following code after the code shown in the figure, replace {Project name} in the code with the Project name >> save

var xmlFile = string.Format(@"{0}/bin/{Project name}.xml",System.AppDomain.CurrentDomain.BaseDirectory);

if (System.IO.File.Exists(xmlFile))
{
    c.IncludeXmlComments(xmlFile);
}
Copy the code

5. Add the ControllerAlready available to skip

5.1 Right-click the Controllers folder >> Select Add >> Select Controllers

5.2 Selecting Web API >> Selecting Web API 2 Controller >> Add >> Enter the Controller name >> Wait until the creation is complete

5.3 Write a simple API in the controller >> Save

6. Use the Swagger

6.1 Ctrl+F5 to start debugging, you should see the following page/your home page

/ Swagger/UI /index >> Enter >> You should be able to Enter the Swagger interface document page

6.3 Click on the name of your new/own controller and you should see a list of interfaces you have written

6.4 Click New/Own Interface, you can see the response type of the interface

6.5 Click Try It Out! >> You can see the interface’s request address, response content, status code, response, and other information