Scott Hanselman

Programmatically adding Mime Types to IIS

August 5, '05 Comments [3] Posted in ASP.NET
Sponsored By

Can you add Mime Types to IIS programmatically? KBs say no: http://support.microsoft.com/kb/q142558/

This VBS will though: (Update: This guy did essentially the same thing!)

Dim LocalMimeMap, MimeMap
Dim ExtensionToAdd, MimeTypeToAdd
Dim i
Const ADS_PROPERTY_UPDATE = 2
Set LocalMimeMap = GetObject("IIS://localhost/MimeMap")
MimeMap = LocalMimeMap.GetEx("MimeMap")
ExtensionToAdd = InputBox("Extension:","IIS") 'TODO Take this from the Command Line
MimeTypeToAdd = InputBox("MIME Type:","IIS") 'TODO Take this from the Command Line
i = UBound(MimeMap)+1
Redim Preserve MimeMap(i) 'Make it bigger and maintain its contents
Set MimeMap(i) = CreateObject("MimeMap") 'Add onto the end
MimeMap(i).Extension = ExtensionToAdd
MimeMap(i).MimeType = MimeTypeToAdd
LocalMimeMap.PutEx ADS_PROPERTY_UPDATE,"MimeMap",MimeMap 'Poke it back in
LocalMimeMap.SetInfo

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. I am a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Saturday, August 06, 2005 3:28:41 PM UTC
Are you sure that this KB says "programatically", I think it says there is no GUI tool for that.
No One
Saturday, August 06, 2005 7:39:11 PM UTC
OK, valid point. Mostly I was just saving this snippet for when I need it again.
Scott Hanselman
Saturday, August 06, 2005 10:14:44 PM UTC
This is particularly helpful if you are planning on using ClickOnce to deploy applications from a server that does not have the .NET Framework 2.0 installed. ClickOnce needs two MIME types that are specific to its operation, which are automatically installed if .NET Framework 2.0 is installed, but will not normally be on other web servers.

The settings are for the ".application" and ".manifest" file extensions, and the MIME type for each should be set to "application/x-ms-application."

Of course you can go through the normal IIS configuration tools, but having the script simplifies things.

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.