WebApi 中使用Swagger
一、NuGet程序包引入Swashbuckle.AspNetCore
二、在Startup启动类中添加swagger服务
public void ConfigureServices(IServiceCollection services) { services.AddControllers();
//添加Swagger服务 services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApiTest", Version = "v1", Description = "我的webapi" }); //var xmlpath = AppDomain.CurrentDomain.BaseDirectory + "webapitest.xml"; //c.IncludeXmlComments(xmlpath);
//以上行是添加接口说明,可以不用
}); }
三、在Startup中添加swagger中间件
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); //添加swagger中间件 app.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApiTest v1")); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
四、在launchSettings.json中配置启动页面