NetCore integrated swagger

1) Use Nuget to search and install Swashbuckle.AspNetCore

 

 

 

2). Register the swagger service in ConfigureServices ()

// Register swagger service 
            services.AddSwaggerGen ((s) => 
            { 
                // The URI friendly name that uniquely identifies the document 
                s.SwaggerDoc ( " swaggerName " , new OpenApiInfo () 
                { 
                    Title = " swagger integration configuration test " , // (required Fill in) the title of the application. 
                    Version = " 5.3.1 " , // (required) version number (the version number of the Swashbuckle.AspNetCore package is written directly here, (v1 is written)) 
                    Description = " description information " , // A short description of the application.
                    Contact = new OpenApiContact () // Open API contact information 
                    { 
                        Email = " [email protected] " , 
                        Name = " 张三" , 
                        Extensions = null , 
                        Url = null 
                    }, 
                    License = new OpenApiLicense () // Open API License information 
                    { 
                        Name = "" Zhang San " , 
                        Extensions = null , 
                        Url = null 
                    } 
                }); 

                // Add Chinese comment 
                 // The path of the XML file generated by stitching 
                var basePath = Path.GetDirectoryName ( typeof (Program) .Assembly.Location);
                 // HomeController is the current A class under the assembly (you can customize a class under the current application assembly) [for getting the assembly name] 
                var commentsFileName = typeof (HomeController) .Assembly.GetName (). Name + " .XML " ;
                 var xmlPath = Path.Combine(basePath, commentsFileName);
                s.IncludeXmlComments(xmlPath);

                s.DocInclusionPredicate((docName, description) => true);

            });

3), use swagger middleware in Configure ()

 

 // Use swagger middleware and provide UI interface 
            app.UseSwagger (); 
            app.UseSwaggerUI ((s) => 
            { 
                // Note: / swagger / URI friendly name that uniquely identifies the document / swagger.josn    
                s.SwaggerEndpoint ( " /swagger/swaggerName/swagger.json " , " Project Name " ); 


            });

 

 Must be consistent with the unique identification name used above

4). Add the Chinese comment in the second step, and also need to configure the xml file that generates the comment

(Right click of project ---------> Properties --------> Generate ----------> (check) XML document file)

 

 5) After checking the output XML document file, it is found that as long as there is no marked class or method, a green wavy warning will appear

 

 At this point, we can cancel the warning plus    ; 1591     to   

 

 6) When moving the project, we want to open the UI interface of swagger by default, rather than manually input each time, so we can configure the default output page

Go to the launchSettings.json configuration file to modify the default output page

 

 Modify the attribute value of the specified node

7), write interface methods (note: using swagger interface methods must clearly mark the request method [common: Get, Post])

/// <summary> 
        /// Get Zhang San's personal information 
        /// </ summary> 
        /// <returns> </ returns> 
        [HttpPost] 
        public JsonResult GetInfo () 
        { 
            return new JsonResult (new 
            { 
                name = " "Zhang San", 
                sex = "Male", 
                age = "20" 
            }); 
        } 

        /// <summary> 
        /// Calculate the sum of a plus b 
        /// </ summary> 
        /// <param name = "a "> First number </ param> 
        /// <param name =" b "> second number </ param> 
        /// <returns> a plus b's equation and </ returns>
        [HttpGet]
        public string GetResult(double a, double b)
        {
            return string.Format($"{a}+{b}={a + b}");
        }

  

 8). Run the project before it is run (you can regenerate it first and output the comment document of "project name.xml" [generated by default in the root directory of the project])

 

 9) Use swagger to test the interface

 9.1), no parameter interface

  

 

 Results of the

 

 9.2), with parameter interface

  

 

 Direct result

 

Guess you like

Origin www.cnblogs.com/licm/p/12719086.html