繁体   English   中英

如何使用Swashbuckle.AspNetCore隐藏响应代码200?

[英]How can I hide response code 200 with Swashbuckle.AspNetCore?

再见,

我正在研究一个ASP.NET Web API核心(目标框架.NET Core 2.1)。 我正在使用Swagger规范记录我的API。 我选择使用Swashbuckle.AspNetCore库。

我有一个简单的创建动作,如下所示:

    /// <summary>
    /// My summary
    /// </summary>
    /// <remarks>My remarks</remarks>
    /// <param name="value">value</param>
    /// <response code="201">Created</response>
    /// <response code="400">Data not valid</response>
    /// <response code="500">Internal Server Error</response>
    [HttpPost]
    public IActionResult Post([FromBody, BindRequired] EntityDto value)
    {
       ...
    }

问题是我生成的swagger.json自动创建了“ 200响应”。 但是因为我的操作仅创建实体,所以它仅返回201响应。 这是以下json片段:

{
  ...
  "responses": {
    "200": {
      "description": "Success"
    },
    "201": {
      "description": "Created"
    },
    "400": {
      "description": "Data not valid"
    },
    "500": {
      "description": "Internal Server Error"
    }
  }
  ...
}

在Internet上冲浪时,我发现了SwaggerResponseRemoveDefaults属性,但似乎仅在Full Framework项目中受支持。

如何删除200条回复?

为了不将默认响应代码包括在内,您可以声明可能的返回码

    /// <summary>
    /// My summary
    /// </summary>
    /// <remarks>My remarks</remarks>
    /// <param name="value">value</param>
    /// <returns>A newly created TodoItem</returns>
    /// <response code="201">Created</response>
    /// <response code="400">Data not valid</response>
    /// <response code="500">Internal Server Error</response>
    [HttpPost]
    [ProducesResponseType(201)]
    [ProducesResponseType(400)]
    [ProducesResponseType(500)]
    public void Post([FromBody] string value)
    {
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM