在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许可证的约束。
- 使用viewmodel中的列表将字典绑定到复选框
- 在ASP.NET 5和MVC 6中从Web项目中分离启动配置的最佳方法
- 无法使用Asp.NET Core中的Entity Framework添加带有视图的Scaffold> MVC Controller
- 在ASP.NET Core MVC API控制器上对AuthorizeAttribute进行unit testing
- 在ASP.NET 5中获取访问令牌
- 从ASP.NET 5控制器VS 2015获取wwwroot文件夹路径
- 如何在ASP.NET Core 1.0的DI中的Startup类中添加IHttpContextAccessor?
- 我应该如何从MVC 6视图中访问我的ApplicationUser属性?
- ASP.NET 5中的dependency injection和对象处理