相关疑难解决方法(0)

在ASP.NET 5中使用MimeMapping(vNext)

我正在尝试将我的旧mvc5项目移动到mvc6.旧代码是:

public string ContentType
{
    get
    {
        if (!string.IsNullOrEmpty(FileName))
            return MimeMapping.GetMimeMapping(FileName);
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

错误是

当前上下文中不存在名称"MimeMapping"

在此输入图像描述

c# system.web mime-types asp.net-core-mvc

32
推荐指数
3
解决办法
1万
查看次数

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)
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

在此输入图像描述

因此,它们之间唯一的区别是[ResponseType(typeof(HttpResponseMessage))]. ASP.NET Core 中有类似的东西吗?

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

5
推荐指数
2
解决办法
1万
查看次数