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

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

再见,

我正在开发 asp.net 网络 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 响应”。但是因为我的操作只创建实体,所以它只有 returns 201 响应。这是以下 json 片段:

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

网上冲浪我发现 SwaggerResponseRemoveDefaults 属性,但似乎它只在完整框架项目中受支持。

如何删除 200 响应?

为了不将默认响应代码包含到 swagger 中,您可以声明可能的 return 代码

    /// <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)
    {
    }