How to register MIME types and file extensions to IIS?

We have a web service, for which to work a particular file extension (.jdf) needs to be registered under MIME types in IIS. Usually what we do is after installing the service (by executing setup file) we go to the particular entry under "Default Web Site" in IIS (We use Windows Vista + IIS 7) and under MIME settings manually add a MIME type. That is, we specify it as follows:

File Extension : .jdf
MIME type : text/xml

Now I'm assigned the task of automating this process. Which means this needs to be done automatically during the installation of the service. The installer is built using C# and Wix, so how can I register this using a C# code?

Please note that I'm just a beginner so any step-by-step instructions and code sample would be of great help. Thanks in advance.

Dani AI

Generated

Short practical answer: on Vista + IIS7 avoid the old ADSI/metabase calls (the IIS://... path). That IIS6-style API is why your code may have “worked once” and then misbehaved. Use the IIS7 configuration API (Microsoft.Web.Administration) or add a <mimeMap> into the site’s staticContent (web.config or applicationHost.config). — if BSSTR is an application under Default Web Site, use the IIS7 location path form ("Default Web Site/BSSTR") rather than an IIS:// metabase path. was pointing you in the right direction toward IIS7 APIs.

A compact, idempotent C# helper you can call from your installer (deferred custom action) — it checks for an existing mapping, updates if needed, and commits:

using System;
using Microsoft.Web.Administration;

public static void EnsureMimeMap(string locationPath, string extension, string mimeType)
{
    using (var sm = new ServerManager())
    {
        var cfg = sm.GetApplicationHostConfiguration();
        var section = cfg.GetSection("system.webServer/staticContent", locationPath);
        var coll = section.GetCollection();

        foreach (ConfigurationElement e in coll)
        {
            if (string.Equals((string)e["fileExtension"], extension, StringComparison.OrdinalIgnoreCase))
            {
                if (!string.Equals((string)e["mimeType"], mimeType, StringComparison.OrdinalIgnoreCase))
                {
                    e["mimeType"] = mimeType;
                    sm.CommitChanges();
                }
                return;
            }
        }

        var map = coll.CreateElement("mimeMap");
        map["fileExtension"] = extension;
        map["mimeType"] = mimeType;
        coll.Add(map);
        sm.CommitChanges();
    }
}

Integration and troubleshooting notes: run this code as a deferred WiX custom action with Impersonate="no" (needs elevated rights). Reference Microsoft.Web.Administration (from %windir%\system32\inetsrv); on 64-bit targets ensure your CA runs in the matching bitness or use appcmd.exe or a web.config fragment as an alternative (dropping a <staticContent><mimeMap ... /></staticContent> into the app’s web.config avoids custom code). Log exceptions, verify the exact locationPath ("Default Web Site/BSSTR" vs just "Default Web Site"), and confirm the mapping in IIS Manager or by inspecting applicationHost.config after install.

Recommended Answers

All 7 Replies

Have a look at this thread.

Have a look at this thread.

adatapost, thanks a lot.
Yes I found that, but I have never used VB before so has some hard time understanding it. It'll be of great help if I can have some C# code.

I have tested that code included with MSDN link - It works fine. You need to add the .NET references of - System.DirectoryServices and a COM reference - Active DS IIS Namespace Provider.

I have tested that code included with MSDN link - It works fine. You need to add the .NET references of - System.DirectoryServices and a COM reference - Active DS IIS Namespace Provider.

I did add them.

Actually, when I execute the code the first time, it registers the extension. However from then on, the second or third time, it doesn't. The code is supposed to delete if it finds, but as I can understand it doesn't.

Btw, I'm not totally clear if the path Im giving is correct. In my IIS Manager, I have a site called "BSSTR" right under "Default Web Site". It is the one I want to set these MIME types. So, the metabase path I gave was :

"IIS://localhost/W3SVC/1/Root/BSSTR"

Is it correct?

How to register MIME types and file extensions to IIS?

we go to the particular entry under "Default Web Site" in IIS (We use Windows Vista + IIS 7) and under MIME settings manually add a MIME type. That is, we specify it as follows:

I missed "Vista + II7" and a note in the link you posted. Please read this blog/article - http://blogs.msdn.com/carlosag/archive/2006/04/17/MicrosoftWebAdministration.aspx and http://msdn.microsoft.com/en-us/magazine/cc163453.aspx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.