39

I added the following to the web.config file, but this seems to be ignored by the development server thats built into Visual Studio 2010. Does anyone know how to alter the MIME types in the development server?

<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true"/>
    <staticContent>
        <mimeMap fileExtension=".mp4" mimeType="video/mp4" />          
        <mimeMap fileExtension=".ogg" mimeType="audio/ogg" />          
        <mimeMap fileExtension=".oga" mimeType="audio/ogg" />          
        <mimeMap fileExtension=".ogv" mimeType="video/ogg" />          
        <mimeMap fileExtension=".webm" mimeType="video/webm" />     
    </staticContent>  
</system.webServer>
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Martin Beeby
  • 4,523
  • 1
  • 26
  • 20
  • Just remembered that I can use IIS Express now in VS2010 SP1, so that got me running. I could have obvoiusly switched to full IIS to solve the issue too... but I am curious to see if there is a way to get MIME types to work with the default developemnt server. – Martin Beeby May 07 '11 at 23:30

3 Answers3

40

The built-in development web server in Visual Studio (Cassini) has no knowledge of <system.webServer>, only IIS7.x or IIS7.5 Express will consume these settings.

Also the static file content types in Visual Studio's development web server are hard coded.

From Microsoft.VisualStudio.WebHost.Connection (disassembled using .NET Reflector):

private static string MakeContentTypeHeader(string fileName)
{
    string str = null;
    FileInfo info = new FileInfo(fileName);
    switch (info.Extension.ToLowerInvariant())
    {
        case ".bmp":
            str = "image/bmp";
            break;

        case ".css":
            str = "text/css";
            break;

        case ".gif":
            str = "image/gif";
            break;

        case ".ico":
            str = "image/x-icon";
            break;

        case ".htm":
        case ".html":
            str = "text/html";
            break;

        case ".jpe":
        case ".jpeg":
        case ".jpg":
            str = "image/jpeg";
            break;

        case ".js":
            str = "application/x-javascript";
            break;
    }
    if (str == null)
    {
        return null;
    }
    return ("Content-Type: " + str + "\r\n");
}

To be honest, with the advent of IIS7.5 Express I can't see why you'd want to use the built-in web server. Cassini can be the cause of so much confusion when it comes to deployment time on a production server because it's nothing like the real deal (security, configuration etc) whereas if you can get your site running on IIS7.5 Express then there's a fairly high probability that deployment onto a production IIS7.5 server will "just work".

I wouldn't be surprised if Microsoft yanked the Cassini server from the next version of Visual Studio given how easy it is to run with IIS7.5 Express.

Kev
  • 118,037
  • 53
  • 300
  • 385
  • 7
    I upvoted your answer because it's accurate, but I absolutely hate it :p I don't really care want to screw around with IIS or anything, I just want it so that when I hit the little green Play button in VS2010, it serves up PNGs with the correct MIME type. Is that too much to ask? Apparently it is... – user435779 Dec 19 '12 at 15:53
  • 2
    @user435779 - Thanks for the +1. IIS Express is fairly pain free and will run up just like Cassini does. There's no mucking about with IIS MMC console. Just tell VS2010 you want to launch and debug in IIS Express (in your project properties Web tab) then press the play button...VS2010 configures IIS Express for you project silently and behind the scenes. – Kev Dec 19 '12 at 15:57
  • This answer was really helpful. As a reassurance to anyone else doing this: I had to restart VS after installing IIS and had a hairy moment where VS crashed after switching to use IIS Express in my project settings, but that aside it was fairly painless and definitely worthwhile. – Jordan Gray Mar 26 '13 at 10:11
  • IIS Express is good and nice, but it's not useful for every situation, especially if you're trying to debug two separate instances app_start and app_end, where both will end if you shut down either one. +1 for a very clear and neat answer. – Crypth Jun 25 '13 at 13:57
  • @Crypth - thanks for the kind words. I agree, there are times when you need to hit up full-fat IIS and Attach to Process to debug some corner cases. – Kev Jun 25 '13 at 17:05
  • @Kev agreed. But it's also useful to have the lightweight cassini collaborating with your IIS/Express for that. – Crypth Jun 26 '13 at 09:05
  • Please correct – nim Dec 27 '14 at 08:08
  • @nim - Can you expand on your comment? Not sure I understand what you're trying to correct. – Kev Dec 28 '14 at 10:59
  • @Kev if server config(Machine.config) contains option for fileExtension=".svg" i have exception. The Must be used to avoid exceptions before – nim Dec 28 '14 at 17:08
  • @nim - I still don't understand what you're getting at or why the svg mime type has anything to do with the question or my answer. – Kev Dec 28 '14 at 18:32
  • @Kev Due to client restrictions we can't install IISExpress. So Can we update static file content types to add svg type? – Ankit Chaudhary May 08 '16 at 13:40
  • @ankitchaudhary - the last time I looked, which was when I wrote this answer, the built-in toy web server provided by Visual Studion hard-codes these mime-types. I don't think anything has changed in VS2015, I don't have time to check or to disassemble the relevant Cassini code. – Kev May 08 '16 at 14:12
4

Just had this issue but had to find the config for IIS Express so I could add the mime types. For me, it was located at C:\Users\<username>\Documents\IISExpress\config\applicationhost.config and I was able to add in the correct "mime map" there.

longda
  • 10,153
  • 7
  • 46
  • 66
  • 1
    This fix only took a few seconds! Thanks! For anybody else trying this, there's a warning to copy a backup of applicationhost.config before you start editing it. – Carl Walsh Nov 19 '13 at 17:50
0

The same can be done by modifying MIME types using IIS Manager as mentioned - Here

Community
  • 1
  • 1
Vintesh
  • 1,657
  • 1
  • 24
  • 31