使用http压缩我意识到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
但是如果我勾选“启用动态内容压缩”文件被压缩:
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
和 ~/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
);
}
我认为这可能是因为 web.config 行是MVC 所需,但也强制所有请求通过 ASP.NET 管道:
<modules runAllManagedModulesForAllRequests="true" />
更新:我尝试将此设置设置为 false 并发生相同的情况。
有办法避免吗?我不想对动态内容进行压缩,但我确实希望对静态内容进行压缩。
或者是将文件放在其他地方的唯一方法?
干杯。
Playing with the httpCompression I relalized that IIS understand static files in MVC as dynamic content, so even if you tick the "Enable static content compression", but don't tick "Enable dynamic content compression", IIS will return the .css
and .js
files without compression:
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
But then if I tick the "Enable dynamic content compression" the files are compressed:
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
Even if I try to ignore the routes to ~/Content
and ~/Scripts
, these files are still understood as dynamic content:
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
);
}
I think this is probably because the web.config line that is needed for MVC but also forces all the request through the ASP.NET pipeline:
<modules runAllManagedModulesForAllRequests="true" />
UPDATE: I have tried to put this setting to false and happens the same.
Is there a way to avoid it? I don't want compression for my dynamic content but I do want it for my static content.
Or is the only way put the files somewhere else?
Cheers.
发布评论
评论(4)
我想您会发现 Rick 已经在这里回答了您的问题:
http://www.west-wind.com/weblog/posts/2011/May/05/Builtin-GZipDeflate-Compression-on-IIS-7x
我不知道为什么你说实话,我们有这个问题。静态压缩在 MVC3 中对我来说是开箱即用的,无需特殊更改。
就像 RickNZ 所说,确保在
applicationhost.config
中正确考虑了 mime 类型。I think you'll find Rick has already answered your question here:
http://www.west-wind.com/weblog/posts/2011/May/05/Builtin-GZipDeflate-Compression-on-IIS-7x
I'm not sure why you are having that issue to be honest. Static Compression is working out of the box for me in MVC3, no special changes needed.
Like RickNZ said, make sure the mime types are accounted for properly in
applicationhost.config
.您可以从 IIS 管理器针对每个文件夹启用动态压缩。首先在“连接”窗格中单击文件夹名称,然后双击中心窗格中的“压缩”图标,然后选择“启用动态压缩”。
或者,这是另一种更暴力的方法:
编辑 C:\Windows\System32\inetsrv\config\applicationHost.config (IIS 配置文件;首先复制)。
在 httpCompression 部分中,删除 mimeType="/" 和 mimeType="text/*" 行,并将其替换为 mimeType="text/css" (JS 条目已经存在) 。
重新启动 IIS 后,动态压缩应该仅应用于您的 CSS 和 CSS。 JS 文件,而不是您的 aspx 输出(即 text/html)。
You can enable dynamic compression on a per-folder basis, from IIS Manager. Click on the folder name first in the Connections pane, then double-click on the Compression icon in the center pane, and select Enable dynamic compression.
Or, here's another, more brute force way:
Edit C:\Windows\System32\inetsrv\config\applicationHost.config (the IIS config file; make a copy first).
In the httpCompression section, remove the lines with mimeType="/" and mimeType="text/*", and replace them with mimeType="text/css" (an entry for JS is already there).
After re-starting IIS, dynamic compression should only be applied to your CSS & JS files, not your aspx output (which is text/html).
IIS 7.5 SP1 或 IIS7 SP1 不再需要。这是 MVC 所必需的,因此对无扩展名 url 的请求将通过 asp.net 管道。
无扩展名 url 支持是 IIS7 SP1 和 IIS7.5 SP1 中的新增功能。
它可以作为 IIS7 的补丁提供,您必须请求并安装。
您可以在这里找到您问题的完整答案:
http://support.microsoft.com/kb/980368
在 IIS 配置中,选中“映射管理器” ”,“路径”栏。也许您对这些文件有映射设置。
还要使用 StaticFileHandler 检查 * 路径。
您是否删除了 web.config 中的任何处理程序?也许通过添加一个声明?
is not required any more for IIS 7.5 SP1 or IIS7 SP1. It was required for MVC so requests to extensionless url go through the asp.net pipeline.
Extensionless url support is new in IIS7 SP1 and IIS7.5 SP1.
It is available for IIS7 as a patch that you have to request and install.
You will find it here with complete answers to your questions:
http://support.microsoft.com/kb/980368
In IIS config, check "mapping manager", "path" column. Maybe you have a mapping setup for these files.
Also check the * path with StaticFileHandler.
Did you remove any handler in your web.config ? Maybe by adding a statement ?
它应该有帮助(IIS7 MVC3):
将另一个映射器添加到您的 web.config
It should help (IIS7 MVC3):
Add another mapper to your web.config