1.添加Swashbuckle.AspNetCore安装包
方式1:使用程序包管理器控制台,安装命令:Install-Package Swashbuckle.AspNetCore
方式2:在Nuget包管理器:输入Swashbuckle.AspNetCore,下载安装。如下图:
2.在Startup 文件中添加配置:
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" }); }); }
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // Enable middleware to serve generated Swagger as a JSON endpoint. app.UseSwagger(); // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint. app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); }); app.UseMvc(); }
3.输入地址访问http://localhost:52408/swagger/访问,效果如下图: