WebApi uses swagger ui to automatically generate interface documents.

Source: Internet
Author: User

WebApi uses swagger ui to automatically generate interface documents.

I wrote it earlier. Webapi is being used recently. Here is a practical example of swageer ui.
Currently, both the frontend and backend are developed separately. Here we provide APIs for the front-end. Sometimes you do not want to write a document specifically for the description of an api. It is a waste of time.
The swagger ui is a Web page that can be integrated into a project so that api comments can be generated. It can be tested and presented to the front end.
Open a ticket.

Step 1 install Nuget
Open your Nuget console and Install-Package Swashbuckle (which project to choose)

Ps. In fact, the first step is complete. You don't need to do anything. After running, enter/swagger/ui/index to view your APIs (without comments ), but it didn't achieve our expected results-the comments were automatically generated to the document. So, continue to look down

 

Step 2 Add the code for generating comments
After installation, the SwaggerConfig. cs class will be added to the App_Start folder. The Register () method in this class will be called when the application starts.
There are a lot of comments in it, the green ones, please forgive him, delete it, delete it, and the rest will be

 1 public static void Register() 2 { 3   var thisAssembly = typeof(SwaggerConfig).Assembly; 4  5   GlobalConfiguration.Configuration 6   .EnableSwagger(c => 7   { 8     c.SingleApiVersion("v1", "WebApplication1"); 9   })10   .EnableSwaggerUi(c =>11   {12 13   });14 }

A little transformation, add a comment to xml

 1 public static void Register() 2 { 3     var thisAssembly = typeof(SwaggerConfig).Assembly; 4  5     GlobalConfiguration.Configuration 6     .EnableSwagger(c => 7     { 8         c.SingleApiVersion("v1", "WebApplication1"); 9         c.IncludeXmlComments(GetXmlCommentsPath());10     })11     .EnableSwaggerUi(c =>12     {    13 14     });15 }16 private static string GetXmlCommentsPath()17 {18     return System.String.Format(@"{0}\bin\WebApplication1.XML", System.AppDomain.CurrentDomain.BaseDirectory);19 }

 

Step 3 required by Step 2
Enable xml document generation, right-click the project file attribute bulid publish -- Output (select XML file)

In fact, swagger relies on the xml generated during build to automatically generate comments on the page.

 

Step 4 is complete. Check the page.


Of course, I am pursuing more than this. We will optimize the optimization.
First, I prefer to add all the configurations to WebApiConfig, which looks clear.

1 public static class WebApiConfig 2 {3 public static void Register (HttpConfiguration config) 4 {5 // Web API configuration and services 6 7 // Web API routes 8 config. mapHttpAttributeRoutes (); 9 10 config. routes. mapHttpRoute (11 name: "DefaultApi", 12 routeTemplate: "api/{controller}/{id}", 13 defaults: new {id = RouteParameter. optional} 14); 15 16 config. registSwagger (); // Add Regist17} 18 private static void RegistSwagger (this HttpConfiguration config) 19 {20 config. enableSwagger ("docs/{apiVersion}/swagger", c => 21 {22 c. singleApiVersion ("v1", "WebApplication1"); 23 c. includeXmlComments (GetXmlCommentsPath (); 24}) 25. enableSwaggerUi (c => 26 {27 28}); 29} 30 private static string GetXmlCommentsPath () 31 {32 return $ @ "{AppDomain. currentDomain. relativeSearchPath} \ WebApplication1.XML "; 33} 34}

Configure the swagger path as well. You can customize it.

1 private static void RegistSwagger (this HttpConfiguration config) 2 {3 config. enableSwagger ("docs/{apiVersion}/swagger", c => 4 {5 c. singleApiVersion ("v1", "WebApplication1"); 6 c. includeXmlComments (GetXmlCommentsPath (); 7}) 8. enableSwaggerUi ("apis/{* assetPath}"); // The original address is/swagger/ui/index, so that the address can be changed to/apis/index 9}

 

In this way, we can use this basic configuration to achieve the expected results-automatically generate an interface document
There should be a lot more configurations here. When I have time to update it, first of all.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.