gpt4 book ai didi

c# - Swashbuckle 自动向生成的 Swagger 文件添加 200 OK 响应

转载 作者:太空狗 更新时间:2023-10-29 17:45:48 27 4
gpt4 key购买 nike

我正在我的 WebApi 2 项目中使用 Swashbuckle 构建 swagger 文档。

我有以下方法定义:

[HttpPost]
[ResponseType(typeof(Reservation))]
[Route("reservations")]
[SwaggerResponse(HttpStatusCode.Created, Type = typeof(Reservation))]
[SwaggerResponse(HttpStatusCode.BadRequest) ]
[SwaggerResponse(HttpStatusCode.Conflict)]
[SwaggerResponse(HttpStatusCode.NotFound)]
[SwaggerResponse(HttpStatusCode.InternalServerError)]
public async Task<HttpResponseMessage> ReserveTickets([FromBody] ReserveTicketsRequest reserveTicketRequest)
{
// ...
return Request.CreateResponse(HttpStatusCode.Created, response);
}

但是生成的 Swagger 文件也包含 HTTP 200 OK,尽管它没有在任何地方指定。

/reservations: 
post:
tags:
- "Booking"
operationId: "Booking_ReserveTickets"
consumes:
- "application/json"
- "text/json"
produces:
- "application/json"
- "text/json"
parameters:
-
name: "reserveTicketRequest"
in: "body"
required: true
schema:
$ref: "#/definitions/ReserveTicketsRequest"
responses:
200:
description: "OK"
schema:
$ref: "#/definitions/Reservation"
201:
description: "Created"
schema:
$ref: "#/definitions/Reservation"
400:
description: "BadRequest"
404:
description: "NotFound"
409:
description: "Conflict"
500:
description: "InternalServerError"
deprecated: false

有没有办法摆脱那个 200 OK?这令人困惑,因为它不是有效的响应。

感谢您的建议。

最佳答案

您可以通过使用 SwaggerResponseRemoveDefaults 属性修饰方法来删除默认响应 (200 OK)。

关于c# - Swashbuckle 自动向生成的 Swagger 文件添加 200 OK 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37813346/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com