2017-09-26 34 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