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

Sau*_*ron 32 c# system.web mime-types asp.net-core-mvc

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

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

错误是

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

在此输入图像描述

Mar*_*k G 54

以下代码应该有效:

string contentType;
new FileExtensionContentTypeProvider().TryGetContentType(FileName, out contentType);
return contentType ?? "application/octet-stream";
Run Code Online (Sandbox Code Playgroud)

  • 注意:在ASPNETCORE 2.1中,此代码要求添加Nuget程序包:“ Microsoft.AspNetCore.StaticFiles”和“使用Microsoft.AspNetCore.StaticFiles;”。宣言。 (4认同)

Mat*_*yas 12

有一个NuGet包MimeTypes可以与.Net Core项目一起使用,作为替代FileExtensionContentTypeProvider.我不知道任何其他mime类型的解析器包,它与.Net Core一起使用(至少到目前为止).

用法很简单:

string fileName = "trial.jpg";
string mime = MimeKit.MimeTypes.GetMimeType(fileName);
Run Code Online (Sandbox Code Playgroud)

  • 使用新版本 https://www.nuget.org/packages/MimeTypes ,它只是 MimeTypes.GetMimeType(filename) (5认同)

Syn*_*der 5

System.Web 没有转移到 .NetCore,因为它过于依赖特定于平台的 API。你可以看看微软的参考资料:

https://github.com/Microsoft/referencesource/blob/master/System.Web/MimeMapping.cs

该代码受 MIT 许可证的约束。