Swagger allows us to focus more on business and code implementation, rather than changing the interface documentation every day! ! ! go
1. Nuget quote:
2. ConfigureServices add service:
1 // Register Swagger 2 services.AddSwaggerGen (c => 3 { 4 c.SwaggerDoc ( " v1 " , new Microsoft.OpenApi.Models.OpenApiInfo 5 { 6 Version = " v1 " , 7 Title = " api document " , 8 Description = " This is a web API based on .net core " , 9 Contact = new Microsoft.OpenApi.Models.OpenApiContact 10 { 11 Name = " Here is the name " , 12 Email = " Here is the mailbox " , 13 Url = new Uri ( " http://www.qufuyun.com " ) 14 }, 15 License = new Microsoft.OpenApi.Models.OpenApiLicense 16 { 17 Name = " Here is the license name " 18 } 19 }); 20 // Set the xml document comment path for Swagger JSON and UI ----- 21 //Get the directory where the application is located (absolute, not affected by the working directory, it is recommended to use this method to get the path) 22 var basePath = Path.GetDirectoryName ( typeof (Program) .Assembly.Location); 23 var xmlPath = Path.Combine (basePath, " ProjectTracker.NetCore.WebUI.xml " ); 24 c.IncludeXmlComments (xmlPath); 25 });
Here, the reference xml document is added to display the API more friendly, and the project properties need to be set:
(The cancel warning code is added because if the xml document is checked, it will keep prompting if the add instruction is not displayed)
3. Configure the request pipeline:
1 // Enable Swagger middleware 2 app.UseSwagger (); 3 // Configure SwaggerUI 4 app.UseSwaggerUI (c => 5 { 6 c.SwaggerEndpoint ( " /swagger/v1/swagger.json " , " My API V1 " ); 7 });
4. The configuration has been completed, you can browse the effect:
Reprint please indicate the source: https://www.cnblogs.com/quluqi/p/12719167.html