Swashbuckle xml 注释中带有与号的查询字符串

lon*_*nix 6 c# swagger swashbuckle

文档为 POST 显示了这一点:

/// <summary>
/// Creates a TodoItem.
/// </summary>
/// <remarks>
/// Sample request:
///
///     POST /Todo
///     {
///        "id": 1,
///        "name": "Item1"
///     }
/// </remarks>
[HttpPost]
public ActionResult<TodoItem> Create(TodoItem item) { }
Run Code Online (Sandbox Code Playgroud)

但是 GET 呢:

/// <summary>
/// Gets a TodoItem.
/// </summary>
/// <remarks>
/// Sample request:
///
///     GET /Todo?iscomplete=true&owner=mike
/// </remarks>
[HttpGet]
public ActionResult<TodoItem> Get(bool isComplete, string owner) { }
Run Code Online (Sandbox Code Playgroud)

问题是这一行中的&符号:/// GET /Todo?iscomplete=true&owner=mike. 编译器抱怨:warning CS1570: XML comment has badly formed XML -- 'Expected an end tag for element 'owner'.'

我也试过了&amp;

我实际上还没有找到 GET 的例子。

什么是正确的语法?

Mat*_*low 6

目前,我们正在使用基于 EspressoBean 的答案但适用于 ASP.NET Core Swashbuckle 库的解决方法。

在您的评论或摘要评论中使用 XML 转义语法:

/// <summary>
/// Gets a TodoItem.
/// </summary>
/// <remarks>
/// Sample request:
///
///     GET /Todo?iscomplete=true&amp;owner=mike
/// </remarks>
Run Code Online (Sandbox Code Playgroud)

在 Startup.cs(ConfigureServices 方法)中添加您的自定义 XmlCommentsEscapeFilter:

        services.AddSwaggerGen(c =>
        {
            ...
            c.OperationFilter<XmlCommentsEscapeFilter>();
        });
Run Code Online (Sandbox Code Playgroud)

添加一个名为 XmlCommentsEscapeFilter.cs 的类:

using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace **MyNamespaceHere**
{
    /// <summary>
    /// Replace &amp; with ampersand character in XML comments
    /// </summary>
    internal class XmlCommentsEscapeFilter : IOperationFilter
    {
        public void Apply(Operation operation, OperationFilterContext context)
        {
            operation.Description = operation.Description?.Replace("&amp;", "&");
            operation.Summary = operation.Summary?.Replace("&amp;", "&");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

为了将来参考,这里是 github 问题的链接(截至 2019 年 8 月 19 日仍然开放):https : //github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1151