有人知道如何在MVC 3中启用gzip压缩吗?我正在使用IIS7。
Google Chrome审核结果:
- 启用gzip压缩(4)
- 使用gzip压缩以下资源可以将它们的传输大小减少约三分之二(~92.23KB):
- /mydomain/ 可以节省 ~1.53KB
- jquery-1.4.4.min.js 可以节省 ~51.35KB
- Cufon.js 可以节省 ~11.89KB
- Futura.js 可以节省 ~27.46KB
有人知道如何在MVC 3中启用gzip压缩吗?我正在使用IIS7。
Google Chrome审核结果:
- 启用gzip压缩(4)
- 使用gzip压缩以下资源可以将它们的传输大小减少约三分之二(~92.23KB):
- /mydomain/ 可以节省 ~1.53KB
- jquery-1.4.4.min.js 可以节省 ~51.35KB
- Cufon.js 可以节省 ~11.89KB
- Futura.js 可以节省 ~27.46KB
您可以通过以下方式通过您的web.config
文件配置压缩:
<system.webServer>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
</system.webServer>
您可以在iis.net/ConfigReference找到此配置元素的文档。这相当于:
注意: (如评论中所指出) 您需要确保已安装 Http 动态压缩,否则设置 doDynamicCompression="true"
将没有任何效果。最快的方法是:
optionalfeatures
(这是访问“打开或关闭Windows功能”窗口的最快方式)optionalfeatures
命令对我无效。相反,需要导航到“服务器管理器” > “角色” > 滚动到“Web 服务器(IIS)”> 点击“添加角色服务”。现在请确保已安装“Web 服务器”>“性能”>“静态内容压缩”和“动态内容压缩”。 - Rory如果你愿意的话,可以通过编程来实现这一点。我会创建一个基础控制器,每个控件都继承自该控制器,并用下面的属性进行装饰。
public class CompressAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var encodingsAccepted = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
if (string.IsNullOrEmpty(encodingsAccepted)) return;
encodingsAccepted = encodingsAccepted.ToLowerInvariant();
var response = filterContext.HttpContext.Response;
if (encodingsAccepted.Contains("deflate"))
{
response.AppendHeader("Content-encoding", "deflate");
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
}
else if (encodingsAccepted.Contains("gzip"))
{
response.AppendHeader("Content-encoding", "gzip");
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
}
}
}
gzip
,而不是 deflate
,在这里阅读更多信息:https://dev59.com/zXRC5IYBdhLWcg3wK9yV#9856879 - Travis J压缩功能在服务器级别上启用/禁用。请参见 IIS 管理控制台中的 IIS 压缩模块。
以下是来自 Microsoft 网站的 IIS 操作说明。