【问题标题】:What is the correct type to include in the ProducesResponseType attribute for an ASP.NET Core API?ASP.NET Core API 的 ProducesResponseType 属性中包含的正确类型是什么?
【发布时间】:2019-09-05 11:32:53
【问题描述】:

在下面有点做作的代码 sn-p 中,我创建了一个控制器方法,它构成了 ASP.NET Core MVC API 的一部分。方法用ProducesResponseType属性修饰,表示响应类型是Stream是否正确,或者响应类型应该是FileStreamResult

[HttpPost("APIFunctionCall")]
[ProducesResponseType(typeof(Stream), 200)]
public async Task<IActionResult> ProcessNewRequestAsync(Request request)
{
    FileStream fs = File.Open(request.Path, FileMode.Open, FileAccess.Read, FileShare.None)
    return returnValue = new FileStreamResult(fs, new MediaTypeHeaderValue("application/ms-word"));
}

【问题讨论】:

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


    【解决方案1】:

    应该是 FileStreamFileStreamResult 而不是我会说

    [ProducesResponseType(typeof(FileStreamResult), 200)]
    

    【讨论】:

    • 两者哪个最好?为什么我会选择指定 FileStreamResult 而不是 FileStream?如果我返回一个 ObjectResult,我会指定封装的类型,那么在这种情况下我也应该更喜欢 FileStream 吗?
    • @EarlSven 好吧,您将返回FileStreamResult,尽管本质上它是FileStream。所以我想说FileStream 可能更有意义。
    • 好的,这是有道理的。谢谢!
    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-23
    相关资源
    最近更新 更多