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

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

public string ContentType { get { if (!string.IsNullOrEmpty(FileName)) return MimeMapping.GetMimeMapping(FileName); return null; } } 

错误是

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

在此处输入图像描述

以下代码应该工作:

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

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

用法很简单:

 string fileName = "trial.jpg"; string mime = MimeKit.MimeTypes.GetMimeType(fileName); 

System.Web不会移动到.NetCore,因为它过分依赖于特定于平台的API。 您可以查看Microsoft参考源:

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

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