2016-06-14 88 views
7

我在我的WebApi 2項目中使用Swashbuckle構建Swagger文檔。Swashbuckle自動添加200 OK響應生成的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); 
} 

但是生成的揚鞭文件包含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?這很混亂,因爲它不是一個有效的迴應。

感謝您的建議。

回答

9

通過使用SwaggerResponseRemoveDefaults屬性修飾方法,可以刪除默認響應(200 OK)。

+0

謝謝!這個竅門! –

+0

有沒有什麼辦法可以在全局添加,而不是在我的控制器中裝飾每種方法? – michaelmsm89

+0

該屬性也可以應用於控制器。要全局添加此行爲,您必須創建自己的'IDocumentFilter' – venerik