我想从给定的扩展名获取MIME Content-Type(最好不访问物理文件).我已经看到一些与此有关的问题,执行此操作的方法可以在下面的文章中继续介绍:

  1. 使用registry information.
  2. 使用urlmon.dll's FindMimeFromData.
  3. 使用IIS information.
  4. 滚动你自己的MIME映射函数.例如,以this table为基础.

我使用no.1已经有一段时间了,但我意识到注册表提供的信息并不一致,这取决于机器上安装的软件.一些扩展,比如.zip不用于指定内容类型.

解决方案2迫使我将文件放在磁盘上,以便读取第一个字节,这样做速度较慢,但可能会获得良好的结果.

第三种方法基于目录服务和所有这些东西,这是我不太喜欢的,因为我必须添加COM引用,我不确定IIS6和IIS7之间是否一致.此外,我不知道这种方法的性能.

最后,我不想使用我自己的表,但如果我想在不同平台(甚至是Mono平台)之间获得良好的性能和一致性,最后似乎是最好的 Select .

你认为有比使用我自己的表或其他描述的方法更好的 Select 吗?你有什么经验?

推荐答案

这取决于您需要MIME类型的目的.通常,对于服务(Web应用程序、Web服务等),建议不要使用依赖于操作系统设置的MIME列表,或者仅在无法以其他方式找到MIME信息时才将其作为后备.

我认为这也是MS Select 在System.Web.MimeMapping类中放入常量MIME类型的原因(不幸的是,不管出于什么原因,它是内部的).

编辑:

Wrapper (<= .NET 3.5)

public static class MimeExtensionHelper
{
    static object locker = new object();
    static object mimeMapping;
    static MethodInfo getMimeMappingMethodInfo;

    static MimeExtensionHelper()
    {
        Type mimeMappingType = Assembly.GetAssembly(typeof(HttpRuntime)).GetType("System.Web.MimeMapping");
        if (mimeMappingType == null)
            throw new SystemException("Couldnt find MimeMapping type");
        ConstructorInfo constructorInfo = mimeMappingType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null);
        if (constructorInfo == null)
            throw new SystemException("Couldnt find default constructor for MimeMapping");
        mimeMapping = constructorInfo.Invoke(null);
        if (mimeMapping == null)
            throw new SystemException("Couldnt find MimeMapping");
        getMimeMappingMethodInfo = mimeMappingType.GetMethod("GetMimeMapping", BindingFlags.Static | BindingFlags.NonPublic);
        if (getMimeMappingMethodInfo == null)
            throw new SystemException("Couldnt find GetMimeMapping method");
        if (getMimeMappingMethodInfo.ReturnType != typeof(string))
            throw new SystemException("GetMimeMapping method has invalid return type");
        if (getMimeMappingMethodInfo.GetParameters().Length != 1 && getMimeMappingMethodInfo.GetParameters()[0].ParameterType != typeof(string))
            throw new SystemException("GetMimeMapping method has invalid parameters");
    }
    public static string GetMimeType(string filename)
    {
        lock (locker)
            return (string)getMimeMappingMethodInfo.Invoke(mimeMapping, new object[] { filename });
    }
}

包装器(.NET 4.0)

public static class MimeExtensionHelper
    {
        static object locker = new object();
        static object mimeMapping;
        static MethodInfo getMimeMappingMethodInfo;

        static MimeExtensionHelper()
        {
            Type mimeMappingType = Assembly.GetAssembly(typeof(HttpRuntime)).GetType("System.Web.MimeMapping");
            if (mimeMappingType == null)
                throw new SystemException("Couldnt find MimeMapping type");            
            getMimeMappingMethodInfo = mimeMappingType.GetMethod("GetMimeMapping", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
            if (getMimeMappingMethodInfo == null)
                throw new SystemException("Couldnt find GetMimeMapping method");
            if (getMimeMappingMethodInfo.ReturnType != typeof(string))
                throw new SystemException("GetMimeMapping method has invalid return type");
            if (getMimeMappingMethodInfo.GetParameters().Length != 1 && getMimeMappingMethodInfo.GetParameters()[0].ParameterType != typeof(string))
                throw new SystemException("GetMimeMapping method has invalid parameters");
        }
        public static string GetMimeType(string filename)
        {
            lock (locker)
                return (string)getMimeMappingMethodInfo.Invoke(mimeMapping, new object[] { filename });
        }
    }

.NET 4.5+

不需要包装器,直接调用公共方法System.Web.MimeMapping.GetMimeMapping.

Asp.net相关问答推荐

Web API 自托管 - 在所有网络接口上绑定

如何从 dll 文件中提取类的源代码?

在构建时自动停止/重新启动 ASP.NET 开发服务器

ASP.NET-MVC 中的表单发布上的 FormCollection 为空

ASP.NET MVC4 jquery/javascript 包的使用

控制器 SessionStateBehavior 是只读的,我可以更新会话变量

如何使图像的一部分成为可点击的链接

asp.net cookie、身份验证和会话超时

通过 jQuery 调用 ASP.NET 服务器端方法

如何在 ASP.Net Core Razor 页面上重定向

在 ASP.NET 中实现 404 的最佳方法

解析器错误消息:无法加载类型网络营销

Web.Config 中的 Assemblies node 的用途是什么?

如何将表从存储过程检索到数据表?

InvalidOperationException:无法为角色创建 DbSet,因为此类型未包含在上下文模型中

带有模型的 ASP.NET MVC 重定向

如何使用 encodeURIComponent() 解码在 JS 中编码的 HTML?

将Bundle 包添加到现有的 ASP.NET Webforms 解决方案

如何清除 System.Runtime.Caching.MemoryCache

Windows 身份验证在本地 IIS 7.5 上不起作用.错误 401.1