1. 我的主机使用 IIS 7,IIS 无权访问设置。 现在 web.config 或其他我发送了一个 js/css/aspx 来应用 Gzip。
2.如何为iis6启用etags
如何为我的网站启用此功能 这行不通(Yslow)
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
<dynamicTypes>
<add mimeType="text/*" enabled="true"/>
<add mimeType="message/*" enabled="true"/>
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="*/*" enabled="false"/>
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true"/>
<add mimeType="message/*" enabled="true"/>
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="*/*" enabled="false"/>
</staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true"/>
编辑
gloabal.asax 中的这段代码很好用,但 Yslow 仍然显示不使用 Gzip??
void Application_PreRequestHandlerExecute(对象发送者,EventArgs e) { HttpApplication app = sender as HttpApplication; string acceptEncoding = app.Request.Headers["Accept-Encoding"]; 流 prevUncompressedStream = app.Response.Filter;
if (!(app.Context.CurrentHandler is Page ||
app.Context.CurrentHandler.GetType().Name == "SyncSessionlessHandler") ||
app.Request["HTTP_X_MICROSOFTAJAX"] != null)
return;
if (acceptEncoding.Contains("gzip") )
return;
acceptEncoding = acceptEncoding.ToLower();
if (acceptEncoding.Contains("deflate") || acceptEncoding == "*")
{
// gzip
app.Response.Filter = new GZipStream(prevUncompressedStream,
CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding", "gzip");
}
else if (acceptEncoding == null || acceptEncoding.Length == 0)
{
// defalte
app.Response.Filter = new DeflateStream(prevUncompressedStream,
CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding", "deflate");
}
}
最佳答案
我认为在代码中实现压缩是错误的方法;我了解您的情况,我相信这很困难,但我会考虑换个房东。
this code in gloabal.asax good work but still Yslow show does not use Gzip??
你的意思是当你在本地测试时它可以工作,但当它部署到主机上时却不行?我肯定已经看到一些代理(例如公司网关)从网络中删除了“accept-encoding gzip”,所以这可能是需要考虑的事情。
关于c# - 如何压缩数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6863043/