.net core interface description using swagger

 First, install the package nuget

Swashbuckle.AspNetCore.Swagger

Swashbuckle.AspNetCore.SwaggerGen

Swashbuckle.AspNetCore.SwaggerUI

1. Add swagger registration code in the configureServices

 1    public void ConfigureServices(IServiceCollection services)
 2         {
 3             services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
 4 
 5             services.AddSwaggerGen(c =>
 6             {
 7                 var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.XML";
 8                 var xmlPath1 = System.IO.Path.Combine(AppContext.BaseDirectory, $"{xmlFile}");
 9 
10                 var xmlModelPath1 = System.IO.Path.Combine(AppContext.BaseDirectory, $" ClassLibrary1.xml " );
 . 11                  c.IncludeXmlComments (xmlPath1);
 12 is                  c.IncludeXmlComments (xmlModelPath1);
 13 is  
14                  c.SwaggerDoc ( " V1 " , new new Swashbuckle.AspNetCore.Swagger.Info
 15                  {
 16                      the Title = " Test " , // modified according to item 
17                      Version = " V1 " ,
 18                      Business Card = new new Swashbuckle.AspNetCore.Swagger.Contact
19                     {
20                         Name = "test",
21                         Email = "[email protected]",
22                         Url = "https://www.test.com"
23                     },
24                     License = new License
25                     {
26                         Name = "内部授权",
27                         Url = "https://www.test.com"
28                     }
29                 });
30             });
31         }
ConfigureServices

2. Use swagger configure the middleware

. 1    public  void the Configure (App IApplicationBuilder, IHostingEnvironment the env)
 2          {
 . 3              IF (env.IsDevelopment ())
 . 4              {
 . 5                  app.UseDeveloperExceptionPage ();
 . 6              }
 . 7  
. 8              app.UseMvc ();
 . 9              // enable generation Swagger middleware services as JSON endpoint 
10              app.UseSwagger ();
 . 11              // enable middleware service swagger-ui, designated Swagger JSON endpoint 
12 is              app.UseSwaggerUI (C =>      // reference Swashbuckle.AspNetCore.SwaggerUI 
13 is              {
 14                 c.SwaggerEndpoint ( " ../swagger/v1/swagger.json " , " O2O the Order the API " ); // the SMS the API is modified to match the App 
15              });
 16          }
Configure

Generate an XML document as a web project and project VModel

ProducesResponseType used on the interface characteristics, information that describes the return value.

 

 • Project start entering http: // localhost: 54022 / swagger / index.html

IP port corresponding to the item into your ip and port

 Opening the post just set the interface to view the information of the parameters and return values

 

 

Guess you like

Origin www.cnblogs.com/jasonbourne3/p/11210811.html