.net core环境配置swagger
一个简单的swagger集成步骤
第一步 引用Nuget包 Swashbuckle.AspNetCore
第二步 在Startup 文件中添加配置
在Startup类的ConfigureServices方法中把Swagger注入进我们的项目中
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "Angel HTTP API",
Description = "The Angel HTTP API",
});
var basePath = AppDomain.CurrentDomain.BaseDirectory;
var apiDocPath = Path.Combine(basePath, "Angel.Api.xml");
options.IncludeXmlComments(apiDocPath);
});
}
在Startup类的Configure方法中添加Swagger相关的中间件
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
//使用Swagger中间件
app.UseSwagger();
app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Angel WebAPI v1"); });
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
第三步 添加xml文件
右键项目》属性》生成》输出,找到【文档文件】勾上,就会显示出【XML文档文件路径】,在里写上Angel.Api.xml,然后F5
第四步 访问swagger UI
https://localhost:8080/swagger/index.html