引入Nuget包
Swashbuckle.AspNetCore.SwaggerGen
Swashbuckle.AspNetCore.SwaggerUI
配置Startup
- 配置
ConfigureServices
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
{
Version = "v1",
Title = ApiName
});
c.OrderActionsBy(a => a.RelativePath);
var xmlFile = "WikiServer.xml";//文件名来源于项目属性==》生成==》输出==》XML文档文件
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
// 启用xml注释. 该方法第二个参数启用控制器的注释,默认为false.
c.IncludeXmlComments(xmlPath, true);
});
- 配置
Configure
app.UseSwagger();
app.UseSwaggerUI(c => {
c.SwaggerEndpoint($"/swagger/v1/swagger.json", $"{ApiName} V1");
//路径配置,设置为空,表示直接在根域名(localhost:8001)访问该文件,注意localhost:8001/swagger是访问不到的,去launchSettings.json把launchUrl去掉,如果你想换一个路径,直接写名字即可,比如直接写c.RoutePrefix = "doc";
c.RoutePrefix = "";
});