Vác*_*uša 9 c# swagger swashbuckle
我在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);
}
Run Code Online (Sandbox Code Playgroud)
但是,生成的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
Run Code Online (Sandbox Code Playgroud)
有没有办法摆脱200 OK?这令人困惑,因为它不是一个有效的回应.
谢谢你的建议.
ven*_*rik 11
您可以通过使用SwaggerResponseRemoveDefaults属性修饰方法来删除默认响应(200 OK).