我正在尝试将我的旧 mvc5项目转移到 asp 网络核心。 旧代码是:
public string ContentType { get { if (!string.IsNullOrEmpty(FileName)) return MimeMapping.GetMimeMapping(FileName); return null; } }
错误是
“ MimeMapping”名称在当前上下文中不存在
系统。网络不会被移动到。因为 NetCore 过于依赖于特定于平台的 API。你可以看看微软的参考资料:
Https://github.com/microsoft/referencesource/blob/master/system 网站/MimeMapping.cs
该代码受麻省理工学院许可证的约束。
以下代码应该可行:
string contentType; new FileExtensionContentTypeProvider().TryGetContentType(FileName, out contentType); return contentType ?? "application/octet-stream";
有一个 NuGet 软件包 模仿类型。作为 FileExtensionContentTypeProvider替代品的 Net Core 项目。我不知道任何其他 mime 类型的解析器软件包,它的工作原理是。网络核心(至少到目前为止)。
FileExtensionContentTypeProvider
用法很简单:
string fileName = "trial.jpg"; string mime = MimeKit.MimeTypes.GetMimeType(fileName);