【问题标题】:Return type of a file for Swagger documentation with dotnet core带有 dotnet core 的 Swagger 文档的文件返回类型
【发布时间】:2018-03-07 19:07:31
【问题描述】:

我正在使用 Swagger for dotnet core 记录我的 dotnet 核心 Web API。

我已阅读文档告诉我需要添加 [ProducesResponseType(typeof(XXXXX),200)]上面的控制器方法,帮助swagger判断方法的响应类型。

我有一个返回文件的控制器方法,我正在尝试弄清楚如何告诉 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[] 但这只是说返回类型是“byte”。

【问题讨论】:

    标签: c# .net-core swagger asp.net-core-webapi


    【解决方案1】:

    您需要的是ProducesAttribute 并指定内容类型作为参数(例如,PDF 文件的“application/pdf”)。

    编辑:看起来 Swagger 可能无法接受 ProducesAttribute。然后我的建议是不要为ProducesResponseType 设置Type,并在方法中添加/// &lt;response code="200"&gt;Returns the requested file&lt;/response&gt; 注释。

    【讨论】:

    • 您认为可以指定所有内容类型吗?
    • 我刚刚尝试过,但它看起来不适用于 aspnetcore 版本的 swashbuckle。
    • 返回文件是响应是一种内容类型,而不是表示 json 或 xml 文档模式的 ProducesResponseType。如果 Swagger 没有接受操作方法上的 ProducesAttribute,那么我只需将 Type 保留为 ProducesResponseType 未设置并使用 &lt;response code="200"&gt;Returns the requested file&lt;/response&gt; 评论
    猜你喜欢
    • 1970-01-01
    • 2013-10-14
    • 2018-07-01
    • 2017-07-30
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    相关资源
    最近更新 更多