【发布时间】:2012-01-08 04:46:59
【问题描述】:
使用 ASP.Net 2.0 和 IIS 6
我通过扩展 IHttpModule 并在 Web.config 文件中注册来启用压缩
public class EnableCompression : IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
}
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpContext context = HttpContext.Current;
String encoding = context.Request.Headers.Get("Accept-Encoding");
if (encoding == null)
return;
encoding.ToLower();
if (encoding.Contains("gzip"))
{
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
HttpContext.Current.Response.AppendHeader("Content-Encoding", "gzip");
}
else
{
context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
HttpContext.Current.Response.AppendHeader("Content-Encoding", "deflate");
}
}
void IHttpModule.Dispose()
{
throw new Exception("The Method or Operation is not Implemented");
}
}
在我还在我的母版页代码文件的页面加载事件中设置过期标头之前,上述工作正常
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
Response.Cache.SetExpires(DateTime.Now.AddMinutes(10));
Response.Cache.SetValidUntilExpires(true);
Response.Cache.SetLastModified(DateTime.Now);
}
设置过期标头后,一旦浏览器从缓存中检索数据,我就会得到一堆垃圾。所以第一次加载页面时,没问题。如果我转到一个新页面然后又回到第一页,我只会得到一个完整的页面:
��`I�%&/m�{J�J��t��`$ؐ@�����iG#)�*�
如果我禁用压缩但保留 expires 标头,那很好 如果我禁用 expires 标头但启用压缩,那很好 如果我同时启用两者,我会得到一个充满垃圾的页面。
我不知道发生了什么。
Response Header
HTTP/1.1 200 OK
Date: Sun, 08 Jan 2012 07:23:15 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Content-Encoding: gzip
Cache-Control: private
Expires: Sun, 08 Jan 2012 07:33:00 GMT
Last-Modified: Sun, 08 Jan 2012 07:23:00 GMT
Vary: *
Content-Type: text/html; charset=utf-8
Content-Length: 13613
Request Header
GET /eng/DE/ HTTP/1.1
Host: wwwnpg
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Cookie: ASP.NET_SessionId=gxdxwnnum5rumue13rxmc5mb
【问题讨论】:
-
这个答案解决了你的问题:stackoverflow.com/a/20253179/1026459
标签: asp.net