【问题标题】:DataAnnotations validation in Web API, how to display in Swagger?Web API中的DataAnnotations验证,如何在Swagger中显示?
【发布时间】:2015-12-01 17:14:51
【问题描述】:

我正在使用 ASP.NET 4.6 和 Web API 2 开发 API。到目前为止,我一直在使用 DTO 和 DataAnnotations,如下所示:

public class CustomerDTO
{
    ...
    [Required]
    public string Name { get; set; }

    [StringLength(20, ErrorMessageResourceName = "Error", ErrorMessageResourceType = typeof(Resources), MinimumLength = 6)]
    public string DocumentNumber { get; set; }
    ...
}

我会像这样使用我的 DTO:

[ResponseType(typeof(CustomerDTO))]
public IHttpActionResult PostCustomer(CustomerDTO customer)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }
    ...
}

现在我被要求发布 API 文档的第一个版本。在做了一些研究之后,我发现了 Swagger,所以我从 NuGet 获得了 Swashbuckle 并开始使用它。顺便说一句,太棒了。

但我确实需要我的 API 用户知道某些参数的最大长度是多少,哪些是必需的,等等。如何让 Swagger 显示此信息?

【问题讨论】:

    标签: asp.net asp.net-web-api documentation swagger swashbuckle


    【解决方案1】:

    查看here答案

    [字符串长度(10,最小长度 = 5)] 公共字符串名称 {get;set;}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多