如何在 AspNetCore 中定义 OpenApi 的文件响应

问题描述 投票:0回答:2

我正在 AspNet Core 5 中编写一个控制器方法,该方法应该以流的形式返回文件。

[HttpGet]
[Route("/docs/{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IActionResult> GetDocumentAsync([FromRoute][Required] Guid? id)
{
    var (stream, fileExtension) = await _dmsService.OpenDocumentAsync(id.Value, FileAccess.Read);
    return File(
        stream,
        contentType: "application/octet-stream",
        fileDownloadName: $"{id}{fileExtension}",
        enableRangeProcessing: false);
}

到目前为止一切正常,但现在我想记录 API。
我使用 Swashbuckle.AspNetCore 6.1.4 生成 OpenAPI 描述,该描述应该用于生成客户端。

当我返回某个对象时,使用

ActionResult<T>
作为返回类型很容易。
但是我如何告诉 Swashbuckle 响应是
stream

c# asp.net-core swagger openapi swashbuckle
2个回答
2
投票
[ProducesResponseType(typeof(FileStreamResult), (int)HttpStatusCode.OK)]

0
投票

我使用 NSWAG 从 MapGet 自动创建客户端代码。

builder.MapGet(
    "download/{path}",
    async ( string path) =>
    {
        var mimeType = "audio/wav";
        return Results.File(path);

    })
    .Produces(StatusCodes.Status404NotFound)
    .Produces((int)StatusCodes.Status200OK, typeof(FileResult))
    .WithName($"GET {EndPointName} by Name")
    .WithTags(EndPointTag);
© www.soinside.com 2019 - 2024. All rights reserved.