首页 > 解决方案 > Asp.Net Core Swagger / FromForm ///(三斜杠)评论没有被拾起?

问题描述

我有一个如下所示的控制器方法:

[HttpPost]
[Consumes("application/x-www-form-urlencoded")]
[Produces("application/json")]
public async Task<IActionResult> GenerateTokenAsync([FromForm]TokenParameters tokenParameters)

TokenParameters 看起来像这样:

public class TokenParameters
{
    /// <summary>
    /// Specifies the grant type. Must be "password".
    /// </summary>
    [Required]
    public GrantType? grant_type
    {
        get;
        set;
    }

    /// <summary>
    /// Specifies the username.
    /// </summary>
    [Required]
    public string username
    {
        get;
        set;
    }

    /// <summary>
    /// Specifies the password.
    /// </summary>
    [Required]
    public string password
    {
        get;
        set;
    }
}

一切正常,但 Swagger UI 没有为成员接收 /// 三斜杠评论。我的其他控制器使用 FromBody 和 /// 三斜杠注释可以很好地处理这些。看起来底部的模型部分接受了评论,但是当我查看控制器时,我正在谈论浅绿色部分中的模型描述。

我查看了模式注册表,描述确实在那里。

编辑:使用 Swashbuckle 5.0 Beta。

编辑#2:它似乎也没有从模式注册表中获取表单参数的示例值。

有任何想法吗?

标签: c#asp.net-coreswaggerswagger-ui

解决方案


确保您的项目已Generate xml documentation选中该选项。

此外,当您配置 Swagger 时,请确保它包含 xml 注释。

// Register the Swagger generator, defining one or more Swagger documents
services.AddSwaggerGen(c =>
{
  c.SwaggerDoc("v2", new Info { Title = "my API", Version = "v2" });

  // Set the comments path for the Swagger JSON and UI.
  var basePath = PlatformServices.Default.Application.ApplicationBasePath;
  var xmlPath = Path.Combine(basePath, "myapp.xml");
  c.IncludeXmlComments(xmlPath);
});

推荐阅读