2017-07-19 130 views
1

Swashbuckle不會生成帶有「UserCreateResponse」輸出的swagger.json,你如何解決這個問題?Swashbuckle - 返回響應的swagger文檔?

[HttpPost] 
    public async Task<IActionResult> Update([FromBody]UserCreate Request) 
    { 
     UserCreateResponse response = new UserCreateResponse(); 

     //do something here 

     // returns UserCreateResponse with http status code 200 
     return Ok(response); 
    } 

你不能做到這一點,因爲它不打算返回的HTTP狀態代碼,200400401等

[HttpPost] 
    public async Task<UserCreateResponse> Update([FromBody]UserCreate Request) 
    { 
     UserCreateResponse response = new UserCreateResponse(); 

     //do something here 

     // returns UserCreateResponse 
     return response; 
    } 

回答

2

您可以指定下列屬性的響應類型:

[ProducesResponseType(typeof(UserCreateResponse), 200)] 
+0

謝謝hezye !!!! – 001

1

另一個變體是使用SwaggerResponse屬性,該屬性還允許提供附加說明:

[SwaggerResponse(HttpStatusCode.OK, "UserDTO", typeof(UserDTO))] 
public async Task<IHttpActionResult> Get([FromODataUri] int key) 
{ 
    var result = await UserRepo.GetAsync(key); 
    ... 
    return Ok(result); 
} 

如下所示產生輸出:

enter image description here enter image description here

它也可以省略類型記錄不返回一個實體的其他狀態碼:

[SwaggerResponse(HttpStatusCode.NotFound, "no data found")] 
[SwaggerResponse(HttpStatusCode.BadRequest, "requiered request headers not found")] 

enter image description here