【问题标题】:ResponseType in ASP.NET CoreASP.NET Core 中的响应类型
【发布时间】:2018-06-27 08:29:11
【问题描述】:

我刚刚将我的项目从 ASP.Net 4.5 移至 ASP.Net Core。 我有一个用于返回 blob 但现在返回 JSON 的 REST API。

这是旧代码:

[HttpGet]
[ResponseType(typeof(HttpResponseMessage))]
[Route("Download/{documentId}")]
public async Task<HttpResponseMessage> DownloadDocument(string documentId)
{
    try
    {
        var result = await TheDocumentService.DownloadDocument(documentId);

        return result;
    }
    catch (Exception ex)
    {
        return new HttpResponseMessage
        {
            StatusCode = HttpStatusCode.InternalServerError,
            Content = new StringContent(ex.Message)
        };
    }
}

除了[ResponseType(typeof(HttpResponseMessage))] 在 ASP.Net Core 中不起作用之外,ASP.net Core 中的代码相同,两种解决方案的返回结果也相同。

但是当在客户端查看来自服务器的响应时,它们会有所不同。

所以它们之间唯一不同的是[ResponseType(typeof(HttpResponseMessage))]。 asp.net core 中有没有等价的东西?

【问题讨论】:

  • 你的控制器顶部有 [Produces("application/json")] 吗?
  • @SBFrancies nope only [HttpGet] [Route("Download/{documentId}")]
  • 我的意思是控制器本身,而不是动作。
  • @SBFrancies 不只是一条路线。
  • 这个问题的答案可能会对你有所帮助:stackoverflow.com/questions/42460198/…

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


【解决方案1】:

How to to return an image with Web API Get method

我通过改变我的回报来解决它:

[HttpGet]
[Route("Download/{documentId}")]
public async Task<IActionResult> DownloadDocument(string documentId)
{
    try
    {
        var result = await TheDocumentService.DownloadDocument(documentId);
        var content = await result.Content.ReadAsByteArrayAsync();
        return File(content, result.Content.Headers.ContentType.ToString());
    }
    catch (Exception ex)
    {
        return StatusCode(500, ex);
    }
}

【讨论】:

  • 不要通过访问Result阻塞异步方法。这是使您的应用程序死锁的好方法。始终使用await,如果这是你的目标,你可以内联,即return File(await result.Content.ReadAsByteArrayAsync()...
【解决方案2】:

编辑:@President Camacho 说 [ResponseType...] 在 .net 核心中不起作用。它已被 [ProducesResponseType] 取代:

[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List<int>))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<IEnumerable<User>>> Get...()

微软的解释很好:https://docs.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-6.0#iactionresult-type 该网站的一句话总结道:“该属性为 Swagger 等工具生成的 Web API 帮助页面生成更多描述性响应详细信息。”

对于其他不想看源代码来开发 UI 的团队成员也有好处。

【讨论】:

  • 您能否解释一下 [ProducesResponseType()] 属性的作用,以及为什么您更喜欢这种语法而不是几年前的 @President Camacho answer 中提供的语法?
  • Microsoft 的解释很好:docs.microsoft.com/en-us/aspnet/core/web-api/… - 网站引述:“此属性为 Swagger 等工具生成的 Web API 帮助页面生成更多描述性响应详细信息。”对于其他不想看源代码来开发 UI 的团队成员来说,这也是一件好事。
  • 提问者说 ResponseType ... 在.net core 中不起作用。对 ProducesResponseType 的引用至少是部分答案。
  • 完美。这正是我想要的。您可以将其编辑到您的答案中吗?这将使它对未来的读者更有用。
【解决方案3】:

Dot Net Core 相当于[ResponseType(typeof(HttpResponseMessage))] [Produces(typeof(HttpResponseMessage))]

【讨论】:

  • 投反对票绝对没问题,但如果能解释原因就更好了。这是一个错误的答案吗?
  • 似乎对我有用?也有兴趣他不投反对票吗?
  • Produces 对于 swagger 文档很有用,但 OP 想要的 api 响应是文件流或 500 错误,而不是 HttpResponseMessage 的 json 序列化。
猜你喜欢
  • 1970-01-01
  • 2020-05-10
  • 1970-01-01
  • 1970-01-01
  • 2019-05-25
  • 1970-01-01
  • 1970-01-01
  • 2020-01-25
  • 1970-01-01
相关资源
最近更新 更多