IIS将MVC中的静态文件理解为动态内容

vto*_*ola 6 asp.net-mvc iis-7 http-compression iis-7.5 asp.net-mvc-3

使用httpCompression我重新认识到IIS将MVC中的静态文件理解为动态内容,因此即使勾选" 启用静态内容压缩 ",但不勾选" 启用动态内容压缩 ",IIS也会返回没有压缩的文件.css.js文件:

GET /MVCX/Content/Site.css HTTP/1.1
Host: localhost
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2
Accept: text/css,*/*;
Referer: http://localhost/mvcx/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

HTTP/1.1 200 OK
Content-Type: text/css
Last-Modified: Mon, 05 Dec 2011 12:42:37 GMT
Accept-Ranges: bytes
ETag: "c79895e4bb3cc1:0"
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Mon, 05 Dec 2011 12:44:43 GMT
Content-Length: 1005
Run Code Online (Sandbox Code Playgroud)

但是,如果我勾选" 启用动态内容压缩 ",文件将被压缩:

GET /MVCX/Content/Site.css HTTP/1.1
Host: localhost
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2
Accept: text/css,*/*;
Referer: http://localhost/mvcx/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

HTTP/1.1 200 OK
Content-Type: text/css
Content-Encoding: gzip
Last-Modified: Mon, 05 Dec 2011 12:42:37 GMT
Accept-Ranges: bytes
ETag: "c79895e4bb3cc1:0"
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Mon, 05 Dec 2011 12:48:36 GMT
Content-Length: 522
Run Code Online (Sandbox Code Playgroud)

即使我试图忽略到~/Content和的路由~/Scripts,这些文件仍然被理解为动态内容:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("{Content}/{*pathInfo}");
        routes.IgnoreRoute("{Scripts}/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    }
Run Code Online (Sandbox Code Playgroud)

我想这可能是因为MVC需要的web.config行,但也通过ASP.NET管道强制所有请求:

<modules runAllManagedModulesForAllRequests="true" />
Run Code Online (Sandbox Code Playgroud)

更新:我试图将此设置设为false并发生相同的情况.

有没有办法避免它?我不希望压缩我的动态内容,但我确实想要它用于我的静态内容.

或者是将文件放在其他地方的唯一方法?

干杯.

Ric*_*kNZ 0

您可以从 IIS 管理器针对每个文件夹启用动态压缩。首先在“连接”窗格中单击文件夹名称,然后双击中心窗格中的“压缩”图标,然后选择“启用动态压缩”。

或者,这是另一种更暴力的方法:

编辑 C:\Windows\System32\inetsrv\config\applicationHost.config(IIS 配置文件;首先复制)。

在 httpCompression 部分中,删除 mimeType=" / " 和 mimeType="text/*"行,并将其替换为 mimeType="text/css" (JS 条目已经存在)。

重新启动 IIS 后,动态压缩应该仅应用于您的 CSS 和 JS 文件,而不是您的 aspx 输出(即 text/html)。