使用ASP.NET Development Server设置MIME类型

Mar*_*eby 37 iis-7 visual-studio-2010 visual-studio mime-types

我将以下内容添加到web.config文件中,但是Visual Studio 2010中内置的开发服务器似乎忽略了这一点.有谁知道如何更改开发服务器中的MIME类型?

<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>
Run Code Online (Sandbox Code Playgroud)

Kev*_*Kev 38

Visual Studio(Cassini)中的内置开发Web服务器不知道<system.webServer>,只有IIS7.x或IIS7.5 Express才会使用这些设置.

此外,Visual Studio的开发Web服务器中的静态文件内容类型是硬编码的.

Microsoft.VisualStudio.WebHost.Connection(使用.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");
}
Run Code Online (Sandbox Code Playgroud)

说实话,随着IIS7.5 Express的出现,我无法理解你为什么要使用内置的web服务器.Cassini在生产服务器上的部署时间可能会引起如此多的混乱,因为它与真正的交易(安全性,配置等)完全不同,而如果你可以让你的网站在IIS7.5 Express上运行,那么它就相当高部署到生产IIS7.5服务器上的概率"正常".

如果微软从下一版本的Visual Studio中抓取Cassini服务器,我会不会感到惊讶,因为运行IIS7.5 Express是多么容易.

  • 我赞成你的答案,因为它准确,但我绝对讨厌它:p我真的不想用IIS或任何东西搞砸,我只是想要它,这样当我点击VS2010中的绿色小按钮时,它就会提供具有正确MIME类型的PNG.那是过分的要求?显然它是...... (6认同)
  • @ user435779 - 感谢+1.IIS Express相当无痛,并且会像Cassini一样运行起来.IIS MMC控制台没有任何问题.只需告诉VS2010你想在IIS Express中启动和调试(在你的项目属性Web选项卡中)然后按下播放按钮...... VS2010为你静默配置IIS Express并在幕后进行项目. (2认同)