简体   繁体   English

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

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

Ciao, 再见,

I'm working on a asp.net web api core (target framework .NET Core 2.1). 我正在研究一个ASP.NET Web API核心(目标框架.NET Core 2.1)。 I'm documenting my API using Swagger specifications. 我正在使用Swagger规范记录我的API。 I chose to use Swashbuckle.AspNetCore library. 我选择使用Swashbuckle.AspNetCore库。

I have one simple create action like the following: 我有一个简单的创建动作,如下所示:

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

The problem is that my generated swagger.json automatically created a "200 response". 问题是我生成的swagger.json自动创建了“ 200响应”。 But because my action only creates entities it only returns 201 response. 但是因为我的操作仅创建实体,所以它仅返回201响应。 This is the following json fragment: 这是以下json片段:

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

Surfing on internet I've found SwaggerResponseRemoveDefaults attribute but seems that it is only supported in Full Framework projects. 在Internet上冲浪时,我发现了SwaggerResponseRemoveDefaults属性,但似乎仅在Full Framework项目中受支持。

How can I remove 200 response? 如何删除200条回复?

In order not to include default response code into swagger, you can declare possible return codes 为了不将默认响应代码包括在内,您可以声明可能的返回码

    /// <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.

相关问题 如何设置 SwashBuckle.AspNetCore.Swagger 以使用授权? - How can I setup SwashBuckle.AspNetCore.Swagger to use Authorization? 如何在 ASP.NET Core Swagger (Swashbuckle.AspNetCore) 中定义控制器描述? - How to define controller descriptions in ASP.NET Core Swagger (Swashbuckle.AspNetCore)? 如何使用 Swashbuckle.AspNetCore 在 Swagger 模式中将自定义泛型类型公开为字符串 - How to expose a custom generic type as a string in Swagger schema using Swashbuckle.AspNetCore 可选参数在Swashbuckle.AspNetCore中导致空异常 - Optional parameter causes null exception in Swashbuckle.AspNetCore Swashbuckle.AspNetCore-无法为继承基本控制器的控制器生成json - Swashbuckle.AspNetCore - Unable to generate json for controllers that inherit base controllers 迁移到 Swashbuckle.AspNetCore 版本 5 时,Swagger UI 中的不记名身份验证 - Bearer authentication in Swagger UI, when migrating to Swashbuckle.AspNetCore version 5 Swashbuckle.AspNetCore openapi 架构中未正确显示可空属性 - Nullable property is not presented in the Swashbuckle.AspNetCore openapi schema properly 无法解析 .net5.0 的“Swashbuckle.AspNetCore (>= 5.6.3)” - Unable to resolve 'Swashbuckle.AspNetCore (>= 5.6.3)' for 'net5.0' 多版本控制 URL 在 Swashbuckle.AspNetCore 5.0.0rc 中不起作用 - Multiple versioning URL not working in Swashbuckle.AspNetCore 5.0.0rc Swashbuckle.AspNetCore (5.0.0-rc5) 忽略公共字段 - Swashbuckle.AspNetCore (5.0.0-rc5) ignoring public fields
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM