簡體   English   中英

Swagger 文檔中未顯示 WebApi 控制器摘要

[英]WebApi controller summary is not showing on Swagger documentation

當我通過 Swagger 啟用此文檔功能時,我能夠查看有關我的文檔的所有類型的信息,但沒有關於我的控制器名稱詳細信息/描述的詳細信息。

如何顯示控制器文檔內容如下例所示?

/// <summary> 

/// Represents the alert api controller class.

/// <summary>

public class XYZController : ApiController
{

}

在啟用 swagger 時,我無法在任何Represents the XYZ api controller class. here地方看到此內容Represents the XYZ api controller class. here Represents the XYZ api controller class. here

但是我能夠看到我的所有方法描述。

您需要在 AddSwaggerGen 中添加 IncludeXmlComments 擴展名,如下所示:

 services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1.0", new Info
            {
                Title = "My APIs",
                Version = "v1.0",
                Description = "REST APIs "
            });

            **// Set the comments path for the Swagger JSON and UI.**
            var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
            var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
            c.IncludeXmlComments(xmlPath);            
        });

有關更多詳細信息,請參閱此處

1.) 右鍵單擊​​項目並編輯 projname.csproj 添加以下內容

<PropertyGroup>
  <GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

2.) 在 ConfigureServices 的 AddSwaggerGen 中添加以下內容

  // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);

有關更多詳細信息,請轉到:

https://docs.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-5.0&tabs=visual-studio

SwaggerConfig 類中是否有以下代碼?

GlobalConfiguration.Configuration 
            .EnableSwagger(c =>
                {

                    c.IncludeXmlComments(string.Format(@"{0}\bin\YourAssemlyName.XML", System.AppDomain.CurrentDomain.BaseDirectory));  

這是可能的,請參閱此頁面上的@Liversage 答案https://github.com/domaindrivendev/Swashbuckle/issues/572

services.AddSwaggerGen(c =>
{
    var xmlPath = ...;
    c.IncludeXmlComments(xmlPath, includeControllerXmlComments: true);
});

由於上面的一些人已經回復了,我想問題是關於這個的:

var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
s.IncludeXmlComments(xmlPath, includeControllerXmlComments: true);

set includeControllerXmlComments: true將允許對控制器進行匯總。

我認為這與以下問題有關: https : //github.com/domaindrivendev/Swashbuckle/issues/572

根據維護者的說法,目前無法顯示控制器摘要:

The reason this has been low on the priority list is because it's not something I've run into issues with. You can absolutely describe what every action in your API does using XML comments on your actions.

就我而言,我只需要標記即可使用 XML 文檔在此處輸入圖片說明

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>C:...\CertReports.Host.xml</DocumentationFile>

現在,在 .net core 中很容易

config.UseControllerSummaryAsTagDescription = true;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM