XML注解没有显示在Swagger文档中,这是Swagger中的bug吗?

jaql4c8m  于 2022-11-06  发布在  其他
关注(0)|答案(4)|浏览(235)

Swagger xml注解没有显示在文档UI中,不确定我在这里遗漏了什么...至少有人告诉我这是一个错误

步骤1:创建一个全新的ASP.NET Web应用程序Web API项目

步骤2:创建Web API项目

步骤3:已安装Swashbuckle 5.6.0 NuGet软件包

步骤4:启用以生成XML文档文件(项目属性-〉生成)

步骤5:更新SwaggerConfig.cs以包含XML注解

public static void Register()
{
    var thisAssembly = typeof(SwaggerConfig).Assembly;

    GlobalConfiguration.Configuration.EnableSwagger(c =>
    {
                var xmlFile = "bin\\" + $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
     });
}

步骤6:已将XML注解添加到控制器

///<Summary>
/// Get these comments1
///</Summary>
public class ValuesController : ApiController
{
    ///<Summary>
    /// Get these comments2
    ///</Summary>
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

WebApplication1.xml也在bin文件夹中生成

<?xml version="1.0"?>
<doc>
    <assembly>
        <name>WebApplication1</name>
    </assembly>
    <members>
        <member name="T:WebApplication1.Controllers.ValuesController">
            <Summary>
             Get these comments1
            </Summary>
        </member>
        <member name="M:WebApplication1.Controllers.ValuesController.Get">
            <Summary>
             Get these comments2
            </Summary>
        </member>
        <member name="M:WebApplication1.Controllers.ValuesController.Get(System.Int32)">
            <Summary>
             Get these comments3
            </Summary>
        </member>
        <member name="M:WebApplication1.Controllers.ValuesController.Post(System.String)">
            <Summary>
             Get these comments4
            </Summary>
        </member>
        <member name="M:WebApplication1.Controllers.ValuesController.Put(System.Int32,System.String)">
            <Summary>
             Get these comments5
            </Summary>
        </member>
        <member name="M:WebApplication1.Controllers.ValuesController.Delete(System.Int32)">
            <Summary>
             Get these comments6
            </Summary>
        </member>
    </members>
</doc>

但是Swagger UI没有显示评论,我不确定我在这里哪里出错了:x1c4d 1x指令集

hm2xizp9

hm2xizp91#

对于.NET 6,请确保配置program.cs

builder.Services.AddSwaggerGen(c =>
{
  c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, 
  $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"));
});

然后在.csproj中添加属性组

<PropertyGroup>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
  </PropertyGroup>
46scxncf

46scxncf2#

试着做
1.删除旧
1.以鼠标器右击按一下[方案总管]中的项目,然后选取[编辑<project_name>.csproj]。
1.将突出显示的行手动添加到.csproj文件中

<PropertyGroup>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
    <NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>

1.生成新文件
https://learn.microsoft.com/en-us/samples/aspnet/aspnetcore.docs/getstarted-swashbuckle-aspnetcore/?tabs=visual-studio

bxjv4tth

bxjv4tth3#

对于.NET 6,另一种方法是基于以下Microsoft文档:
打开项目文件.csproj并添加以下行

<PropertyGroup>
  <GenerateDocumentationFile>true</GenerateDocumentationFile>
  <NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>

然后在程序文件中更改此设置:

builder.Services.AddSwaggerGen();

到这

builder.Services.AddSwaggerGen(options =>
{
    // using System.Reflection;
    var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
    options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});
lp0sw83n

lp0sw83n4#

在我的例子中,我设置了always copy for xml generated at build选项。
并且在启动时在AddSwaggerGen选项处添加一些代码,如果文件存在,则通过IncludeXmlComments生成注解:

services.AddSwaggerGen((opt) =>
        {
            opt.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1",
                Description = "Your app desc",
                Contact = new OpenApiContact()
                {
                    Name = "@yourname",
                    Url = new System.Uri("yoursite.com"),
                }
            });

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

            if (File.Exists(xmlPath))
                opt.IncludeXmlComments(xmlPath);
        });

相关问题