通过使用 httpCompression,我意识到 IIS 将 MVC 中的静态文件理解为动态内容,因此即使您勾选了“启用静态内容压缩“,但不要勾选”启用动态内容压缩", IIS 将返回.css
and .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
但如果我勾选“启用动态内容压缩“文件被压缩:
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
即使我试图忽略去往的路线~/Content
and ~/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
);
}
我认为这可能是因为 MVC 需要 web.config 行,但也强制所有请求通过 ASP.NET 管道:
<modules runAllManagedModulesForAllRequests="true" />
更新:我尝试将此设置设置为 false 并且发生相同的情况。
有办法避免吗?我不想对动态内容进行压缩,但我确实希望对静态内容进行压缩。
或者是将文件放在其他地方的唯一方法?
Cheers.