【问题标题】:XML comments are not shown in Swagger documentation, is this bug in swagger?Swagger 文档中没有显示 XML 注释,这是 swagger 中的错误吗?
【发布时间】:2022-01-20 16:10:25
【问题描述】:

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

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

第 2 步:创建 Web API 项目

第三步:安装 Swashbuckle 5.6.0 NuGet 包

Step4:启用生成 XML 文档文件(项目属性 -> 构建)

第 5 步:更新 SwaggerConfig.cs 以包含 XmlComments

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);
     });
}

第六步:向控制器添加 XML cmets

///<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 没有显示 cmets,我不确定我在哪里出错了:

【问题讨论】:

  • 在xmlPath变量处下断点,检查路径是否正确
  • XML 路径是正确的,我确实看到了在 bin 文件夹中创建的带有 cmets 的 XML 文件
  • 第一步是创建 ASP.NET Core 项目,但在将 Swashbuckle 包添加到 ASP.NET Web API 时会创建 SwaggerConfig.cs。如果您使用的是 Core,请使用 Swashbuckle.AspNetCore 包
  • @YegorAndrosov 你找到解决方案了吗?
  • @GuilhermeWaltricke OP 的问题是因为他们使用的是 ASP.NET 包而不是 Core

标签: asp.net-core swagger swagger-2.0


【解决方案1】:

尝试做

  1. 删除旧的
  2. 在解决方案资源管理器中右键单击项目并选择编辑&lt;project_name&gt;.csproj
  3. 手动将突出显示的行添加到.csproj 文件中
<PropertyGroup>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
    <NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
  1. 生成新文件

https://docs.microsoft.com/en-us/samples/aspnet/aspnetcore.docs/getstarted-swashbuckle-aspnetcore/?tabs=visual-studio

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    相关资源
    最近更新 更多