Swagger 文档中未显示 WebApi 控制器摘要

JAR*_*VIS 14 swagger swagger-ui asp.net-web-api2

当我通过 Swagger 启用此文档功能时,我能够查看有关我的文档的所有类型的信息,但没有关于我的控制器名称详细信息/描述的详细信息。

如何显示控制器文档内容如下例所示?

/// <summary> 

/// Represents the alert api controller class.

/// <summary>

public class XYZController : ApiController
{

}
Run Code Online (Sandbox Code Playgroud)

在启用 swagger 时,我无法在任何地方看到此内容 Represents the XYZ api controller class. here

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

Asa*_*lik 13

这是可能的,请参阅本页上的@Liversage答案https://github.com/domaindrivendev/Swashbuckle/issues/572

services.AddSwaggerGen(c =>
{
    var xmlPath = ...;
    c.IncludeXmlComments(xmlPath, includeControllerXmlComments: true);
});
Run Code Online (Sandbox Code Playgroud)


Pal*_*Roy 11

您需要在 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);            
        });
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参阅此处


小智 10

1.) 右键单击​​项目并编辑 projname.csproj 添加以下内容

<PropertyGroup>
  <GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请转到:

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

  • 这是对我帮助最大的一个。 (2认同)

lev*_*ent 5

SwaggerConfig 类中是否有以下代码?

GlobalConfiguration.Configuration 
            .EnableSwagger(c =>
                {

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


小智 5

楼上几位已经回复了,我猜问题是这样的:

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

setincludeControllerXmlComments: true将允许获取控制器的摘要。