How to add mime types with IIS7 Web.config

Posted: Mar 25, 2008  7 comments  

Average Rating

Tags
Administration
Administrators
Configuration
Developers
IIS7
Tutorial

Share this Post

Ever wanted to add a custom mime type to your Web server?  I ran into this issue the other day when I tried to serve up .mp4 files from my Web server and duh, I got this error:

404.3 error

404.3 error - mime type missing!

Why does IIS block requests for unknown mime types?  Well, unlike some web servers, we believe it is irresponsible to serve out random content.  After all, did you really intend to serve up that .mdb (access database), .passwd (password), .inc (source include) or other random files that may have landed in your web content directory?  We really don't know, so we error on the safe side and block all unknown extensions by default from being served.  To make it easy to troubleshoot, we return this special error - coded 404.3. 

Thankfully, adding mime types is easier than ever thanks to the all-new distributed configuration option, which allows for IIS7 configuration to be stored in web.config files, along with asp.net configuration, to be deployed with your content.  This makes transferring IIS7 configuration from your Vista PC to your hosted server as easy as copying files!  Read more about this in the Delegating Configuration section of http://learn.IIS.net

In this post, I'll show how easy it is to add mime types to your Web server.  This method will work on any IIS7 web server, and it will be ignored on all non-IIS7 web servers, so it should be safe to do no matter the type of application or content.  Since the <staticContent> section is delegated by default, the configuration snippets below should 'just work' on all IIS7 Web sites. 

Scenario:  Let's say I want serve up some h264 video and need to add the .mp4 and .m4v file types to IIS7?  It's as easy as:

1) create (or edit) the web.config file in your site's home directory

2) edit it as follows:

<configuration>

    <system.webServer>

        <staticContent>
            <mimeMap fileExtension=".mp4" mimeType="video/mp4" />
            <mimeMap fileExtension=".m4v" mimeType="video/m4v" />
     </staticContent>

    </system.webServer>

</configuration>

 

Scenario 2: Let's say I want to add Silverlight-related mime types to my site/application?  See this article for all the gory details, but it is as easy as:

1) create (or edit) the web.config file in your site's home directory

2) edit it as follows:

<configuration>

    <system.webServer>

        <staticContent>

<mimeMap fileExtension=".xaml" mimeType="application/xaml+xml" />
<mimeMap fileExtension=".xap" mimeType="application/x-silverlight-app" />
<mimeMap fileExtension=".xbap" mimeType="application/x-ms-xbap" />

     </staticContent>

    </system.webServer>

</configuration>

Also, if you'd like a fancy UI to help you to manage mime types, IIS Manager provides a nice tool based approach.  Just click on the 'mime types' feature:

image 

and then click 'add' on the Actions pane:

image

Whoila!

Comments

And how about .php files? How can i change the mimi enabling IIS to execute these files/

Thanks and regards,

Robert

robert@rcdeleeuw.nl

Mar 26 2008 by Robert Chr. de Leeuw

Hi Robert -

Mime types are configured for static content, so the Web server can alert the client (browser) in the header of the response what type of data to expect and render.  Mime types *are not* used for dynamic content, so you should never configure the Web server mime types to serve PHP, or IIS is likely to serve out your source code as plain text.  

To configure PHP on IIS7, see this article:  learn.iis.net/.../using-fastcgi-to-host-php-applications-on-iis7

To configure PHP on IIS6, see this article: learn.iis.net/.../using-fastcgi-to-host-php-applications-on-iis-60

Mar 26 2008 by bills