.NET6 Swagger 顯示注釋


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發布到站點下。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM