WEB API configuration Swagger

Swagger

Swagger as an online document, generate a Json string data by back-end interface controller, real-time display interface request back-end address, parameters, and a callback type, a good solution to this problem (

NuGet reference to third-party libraries

Tools -> NuGet Package Manager -> NuGet management solution package ...
in the browser to find "Swashbuckle.AspNetCore", select the project, click Install.

Add NuGet package

 

 

 

 

 

 

 

 

 

 

 

In Startup.cs ConfigureServices file, add the following code:

        public  void ConfigureServices (IServiceCollection Services) 
        { 
            services.AddMvc () SetCompatibilityVersion (CompatibilityVersion.Version_2_2);. 
            #region Swagger 
            services.AddSwaggerGen (Options => 
            { 
                options.SwaggerDoc ( "V1", new new Info 
                { 
                    Version = "V1.1.0" , 
                    the Title = "WebAPI", 
                    the Description = "test Swagger", 
                }); 

                // set the UI xml documentation comments and JSON path Swagger 
                var basePath = Path.GetDirectoryName (AppContext.BaseDirectory); // get the application directory (absolute , from the working directory, we suggest using this method to obtain the path)
                var xmlPath = Path.Combine(basePath, "DemoTest.xml");
                options.IncludeXmlComments(xmlPath);

            }); 
            #endregion 
        }

 

In Startup.cs Configure class method, add the following code:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
           …
           
            #region Swagger
            app.UseSwagger();
            app.UseSwaggerUI(options =>
            {
                options.SwaggerEndpoint("/swagger/v1/swagger.json", "Web API Help V1");
            });
            #endregion

            app.UseHttpsRedirection();
            app.UseMvc();
        }

Right current project properties, select Generate columns

 Write controller to see results

 Build the project view, browser and enter the port number / /swagger/index.html

 

 

 

Guess you like

Origin www.cnblogs.com/876878958-/p/11563168.html