我正在努力实现 Steve Souders 讨论的内容 http://www.stevesouders.com/blog/2010/07/12/velocity-forcing-gzip-compression/关于强制gzip压缩
我有一个运行这个的模块:
void context_PreSendRequestHeaders(object sender, EventArgs e)
{
var app = sender as HttpApplication;
var request = app.Request;
var response = app.Response;
if (CompressionUtils.GzipSupported(request) || CompressionUtils.GzipNotSupportedExplicitly(request))
{
return;
}
if (CompressionUtils.GzipSupportedExplicitly(request))
{
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
response.AddHeader(HttpHeaderKey.ContentEncoding, "gzip");
return;
}
response.Write("<iframe style=\"display:none;\" src=\"/CompressedPage.aspx\"></iframe>");
}
CompressionUtils.GzipSupported
仅检查“accepts-encoding” header ,同时
CompressionUtils.GzipSupportedExplicitly
和 CompressionUtils.GzipNotSupportedExplicitly
检查说明浏览器是否真的可以读取 gzip 的 cookie
但是当我在 Firefox 中加载一个页面时,我得到了这个错误:
Content Encoding Error
The page you are trying to view cannot be shown because it uses an invalid or unsupported form of compression.
在 Fiddler 中它显示已添加内容编码 header 但内容尚未压缩
最佳答案
事实证明我只是绑定(bind)得太晚了,绑定(bind)到 PostMapRequestHandler
而不是 PreSendRequestHeaders
。现在工作正常。
关于c# - 我正在尝试使用 GZipStream 在页面上强制执行 gzip 压缩,但浏览器显示我正在使用不受支持的压缩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3410608/