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.
In the Connections pane, go to the site, application, or directory for which you want to add a MIME type. In the Home pane, double-click MIME Types. In the MIME Types pane, click Add... in the Actions pane. In the Add MIME Type dialog box, add the file name extension and MIME type, and then click OK.
When a browser receives a document from web-server, it reads content-type header and decides how to parse a file. I can explicitly set correct content-type for headers from scripts on server, but when web-server serves css or javascript files, it automatically sets correct mime-types.
In IIS Manager, right-click the local computer, and click Properties. Click the MIME Types button.
MIME stands for "Multipurpose Internet Mail Extensions." It is a standard way of classifying file types on the Internet. By specifying a MIME type, application can easily identify the type of file and can extract more information and attributes about a file.
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";
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With