1、修改Program.cs
這行代碼默認就有:
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
重點是這里的代碼:
builder.Services.AddSwaggerGen((options) =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Y核心API", Version = "v1.0", Description = "", });
var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//獲取應用程序所在目錄
var xmlPath = Path.Combine(basePath, $"{Assembly.GetExecutingAssembly().GetName().Name}.xml");//接口action顯示注釋
options.IncludeXmlComments(Path.Combine(basePath, "Y.WebAPI.xml"), true);//接口注釋
options.IncludeXmlComments(Path.Combine(basePath, "Y.API.Application.xml"), true);//實體類注釋
});
2、修改項目屬性
設置生成xml文檔
3、運行
這個時候swagger就能看到效果了,注意一點,發布的時候會生成.xml文件,SIT測試環境如果要用swagger也需要把這個xml發布到站點下。