ASP.NET/IIS6: How to search the server's mime map?
Asked Answered
C

3

11

i want to find the mime-type for a given file extension on an IIS ASP.NET web-server from the code-behind file.

i want to search the same list that the server itself uses when serving up a file. This means that any mime types a web-server administrator has added to the Mime Map will be included.

i could blindly use

HKEY_CLASSES_ROOT\MIME\Database\Content Type

but that isn't documented as being the same list IIS uses, nor is it documented where the Mime Map is stored.

i could blindly call FindMimeFromData, but that isn't documented as being the same list IIS uses, nor can i guarantee that the IIS Mime Map will also be returned from that call.

Chalcopyrite answered 6/10, 2008 at 15:55 Comment(0)
U
10

Here's one I made earlier:

public static string GetMimeTypeFromExtension(string extension)
{
    using (DirectoryEntry mimeMap = 
           new DirectoryEntry("IIS://Localhost/MimeMap"))
    {
        PropertyValueCollection propValues = mimeMap.Properties["MimeMap"];

        foreach (object value in propValues)
        {
            IISOle.IISMimeType mimeType = (IISOle.IISMimeType)value;

            if (extension == mimeType.Extension)
            {
                return mimeType.MimeType;
            }
        }

        return null;

    }
}

Add a reference to System.DirectoryServices and a reference to Active DS IIS Namespace Provider under the COM tab. The extension needs to have the leading dot, i.e. .flv.

Unknown answered 6/10, 2008 at 16:17 Comment(2)
Note that this code doesn't seem to function in IIS 7.5, at least not when using the default configuration. Works fine in IIS 6.0.Bookbindery
@Blinky - The OP asked about IIS6 (see the title) and I didn't make any suggestion that this would work on IIS7. IIS7 != IIS6, they are two different web servers. It might work if the IIS6 backwards compatibility bits are installed. But you wouldn't do it this way on IIS7 if they were, you'd search the <mimeMap> setting using the managed API: iis.net/ConfigReference/system.webServer/staticContent/mimeMapUnknown
D
12

Here is another similar implementation, but doesn't require adding the COM reference - it retrieves the properties through reflection instead and stores them in a NameValueCollection for easy lookup:

using System.Collections.Specialized; //NameValueCollection
using System.DirectoryServices; //DirectoryEntry, PropertyValueCollection
using System.Reflection; //BindingFlags

NameValueCollection map = new NameValueCollection();
using (DirectoryEntry entry = new DirectoryEntry("IIS://localhost/MimeMap"))
{
  PropertyValueCollection properties = entry.Properties["MimeMap"];
  Type t = properties[0].GetType();

  foreach (object property in properties)
  {
    BindingFlags f = BindingFlags.GetProperty;
    string ext = t.InvokeMember("Extension", f, null, property, null) as String;
    string mime = t.InvokeMember("MimeType", f, null, property, null) as String;
    map.Add(ext, mime);
  }
}

You can very easily cache that lookup table, and then reference it later:

Response.ContentType = map[ext] ?? "binary/octet-stream";
Dethrone answered 10/2, 2010 at 22:28 Comment(1)
This doesn't appear to work with IIS Express -- you'll get a "System.Runtime.InteropServices.COMException (0x80070005): Access is denied." when accessing entry.Properties["MimeMap"].Ashil
U
10

Here's one I made earlier:

public static string GetMimeTypeFromExtension(string extension)
{
    using (DirectoryEntry mimeMap = 
           new DirectoryEntry("IIS://Localhost/MimeMap"))
    {
        PropertyValueCollection propValues = mimeMap.Properties["MimeMap"];

        foreach (object value in propValues)
        {
            IISOle.IISMimeType mimeType = (IISOle.IISMimeType)value;

            if (extension == mimeType.Extension)
            {
                return mimeType.MimeType;
            }
        }

        return null;

    }
}

Add a reference to System.DirectoryServices and a reference to Active DS IIS Namespace Provider under the COM tab. The extension needs to have the leading dot, i.e. .flv.

Unknown answered 6/10, 2008 at 16:17 Comment(2)
Note that this code doesn't seem to function in IIS 7.5, at least not when using the default configuration. Works fine in IIS 6.0.Bookbindery
@Blinky - The OP asked about IIS6 (see the title) and I didn't make any suggestion that this would work on IIS7. IIS7 != IIS6, they are two different web servers. It might work if the IIS6 backwards compatibility bits are installed. But you wouldn't do it this way on IIS7 if they were, you'd search the <mimeMap> setting using the managed API: iis.net/ConfigReference/system.webServer/staticContent/mimeMapUnknown
I
1

IIS stores the MIME information in its own database. Searching for "MimeMap IIS" on the internet will reveal how to read it or even change it. See for example C# - How to display MimeMap entries to the console from an instance of IIS.

Insane answered 6/10, 2008 at 16:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.