c# - ASP.NET Core 中的响应类型

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

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

这是旧代码:

[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 中的代码是相同的,而且两种解决方案中的返回结果也是相同的.

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

enter image description here

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

最佳答案

Dot Net Core 等价于 [ResponseType(typeof(HttpResponseMessage))] [Produces(typeof(HttpResponseMessage))]

关于c# - ASP.NET Core 中的响应类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48318381/

相关文章:

c# - 检查列表中的下一个最高/最低值并选择该项目

c# - Visual Studio 2015 扩展方法调用作为方法组

c# - 在 asp.net c# 中使用 outlook express 发送电子邮件

c# - 缓存键导致错误 "Negating the minimum value of a twos complement number is invalid."

c# - 为什么在 MVC 4 中通过 CSS 在文本框的一 Angular 不显示图像?

c# - 无法在 Sitefinity MVC Controller 操作中使用异步/等待

c# - 有什么简单的方法可以将 XML 输出从共享点 GetListItems() 转换为 DataTable

asp.net - 为什么我的 ASPX 页面在编译后加载速度如此之慢?

jquery - 单击日历时将数据加载到部分 View (ASP.NET MVC)

c# - 局部 View 中的 ASP.NET MVC 验证并返回到父 View