Scott Hanselman

Programmatically adding Mime Types to IIS

August 05, 2005 Comment on this post [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. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
August 06, 2005 19:28
Are you sure that this KB says "programatically", I think it says there is no GUI tool for that.
August 06, 2005 23:39
OK, valid point. Mostly I was just saving this snippet for when I need it again.
August 07, 2005 2:14
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.