Function and configuration of swagger

Time:2022-5-4

In learning net core, I came into contact with swagger, learned and recorded

Introducing swagger into pure api projects can generate visual API interface pages

官方样式   

新建项目的默认样式


 

Introduction package

Nuget package:Swashbuckle. Aspnetcore (latest stable version)

to configure

1. Configure the startup class configureservices method

Function and configuration of swaggerFunction and configuration of swagger

1 public void ConfigureServices(IServiceCollection services)
 2         {
 3 // swagger service configuration
 4             services.AddSwaggerGen(c =>
 5             {
 6                 c.SwaggerDoc("V1", new Microsoft.OpenApi.Models.OpenApiInfo
 7                 {
 8 version = "V1", // interface document version
 9 title = "my interface document 1.0", // interface document title
10 description = "my first swagger document", // interface document description
11                     Contact = new Microsoft. OpenApi. Models. Openapicontact {name = "Zhang Hua", url = new URI(“ http://baidu.com "), Email = " [email protected] " },
12                     License = new Microsoft. OpenApi. Models. Openapilicense {name = "Zhang Hua", url = new URI(“ http://baidu.com ") }
13                 });
14             });
15             services.AddControllers();
16         }

1. Configure the startup class configureservices method

2. Configure the middleware of startup class and configure method

Function and configuration of swaggerFunction and configuration of swagger

1 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
 2         {
 3             if (env.IsDevelopment())
 4             {
 5                 app.UseDeveloperExceptionPage();
 6             }
 7 
 8             app.UseRouting();
 9 
10             app.UseAuthorization();
11 
12             app.UseEndpoints(endpoints =>
13             {
14                 endpoints.MapControllers();
15             });
16 
17 // swagger middleware startup configuration
18             app.UseSwagger();
19             app.UseSwaggerUI(a => {
20 a. swaggerendpoint ("/ swagger / V1 / swagger. JSON", "middleware startup configuration, my first swagger document");
// if the root domain name is / 21, the access path is empty HTML. Note that localhost: 8001 / swagger is inaccessible
22 // the path configuration is set to null, which means that the file is accessed directly in the root domain name (localhost: 8001)
23                 // c.RoutePrefix = "swagger"; //  If you want to change a path, just write the name directly, such as c.routeprefix = "swagger"; The access path is the root domain name / swagger / index html
24                 a.RoutePrefix = string. Empty;// route
25             });
26         }

2. Configure the middleware of startup class and configure method

be careful:

When the new project is configured and run for the first time, it may be as follows. because/Weatherforecast is the official default address

Solution: launchsettings. In the properties folder The launchurl property of the JSON file is changed to null

Launchurl represents the relative URL to launch in the browser


PS: little personal understanding. I hope I can correct any mistakes