2017-09-26 35 views
3

我使用Swagger for dotnet core來記錄我的dotnet核心Web API。使用dotnet核心的Swagger文檔的返回類型

我已閱讀文檔,告訴我需要在控制器方法上方添加 [ProducesResponseType(typeof(XXXXX),200)]以幫助確定方法的響應類型。

我有一個控制器方法,返回一個文件,我想解決如何我可以告訴swagger我返回一個文件。

public class DocumentController : Controller 
{ 
    private readonly IDocumentService _documentService; 

    public DocumentController(IDocumentService documentService) 
    { 
     _documentService = documentService; 
    } 

    [HttpGet("{documentId}", Name= DocumentRoutes.Document)] 
    [ProducesResponseType(typeof(XXXXX), 200)] // <== What goes here? 
    public async Task<IActionResult> GetDocument(Guid documentId) 
    { 
     DocumentAdto documentAdto = await _documentService.GetAsync(documentId); 
     return File(documentAdto.DocumentBytes, documentAdto.ContentType, documentAdto.Name); 
    } 
} 

有沒有人有任何想法?

我想過byte [],但只是說返回類型是「字節」。

回答

4

您需要的是ProducesAttribute並指定內容類型作爲參數(例如PDF文件的「application/pdf」)。

編輯:它似乎Swagger可能不拿起ProducesAttribute。我的建議是將Type取消設置爲ProducesResponseType,併爲該方法添加/// <response code="200">Returns the requested file</response>註釋。

+0

你認爲你可以指定所有的內容類型嗎? – chris31389

+0

我剛剛嘗試過這一點,它看起來沒有使用aspnetcore版本的swashbuckle。 – chris31389

+0

返回一個文件是一個響應是一種內容類型,而不是'ProducesResponseType',它表示一個json或xml文檔模式。如果Swagger沒有選擇Action方法中的'ProducesAttribute',那麼我會簡單地將'Type'替換爲'ProducesResponseType'並且使用'返回請求的文件'comment – Moho