2

我正在实施页面/资源压缩以提高网站性能。

我试图同时实现吹牛和邪恶的 HttpCompress,但最终得到了相同的结果。这似乎只影响 Firefox,我在 Chrome 和 IE 上测试过。

发生的情况是我第一次请求页面所有外部资源解压正常。页面第二次或第三次出现错误,因为资源似乎没有被解压缩。我得到 unicode 字符,例如:

������í½`I%&/mÊ{JõJ×àt¡`$Ø@ìÁÍæìiG#)«*ÊeVe]f

(实际上这里不能正常显示)

通过 firebug 检查页面显示响应标头为:

缓存控制私有

内容类型文本/html;字符集=utf-8

内容编码 gzip

服务器 Microsoft-IIS/7.5

X-AspNetMvc-2.0 版

X-AspNet-版本 2.0.50727

X-Compressed-By HttpCompress

X-Powered-By ASP.NET 日期 7 月 9 日星期五

2010 06:51:40 GMT 内容长度 2622

这清楚地表明该资源是由 gzip 压缩的。那么客户端的放气方面似乎出了点问题?

我在 web.config 中添加了以下部分(在适当的位置):

<sectionGroup name="blowery.web">
  <section name="httpCompress" type="blowery.Web.HttpCompress.SectionHandler, blowery.Web.HttpCompress"/>
</sectionGroup>

<blowery.web>
    <httpCompress preferredAlgorithm="gzip" compressionLevel="high">
      <excludedMimeTypes>
        <add type="image/jpeg"/>
        <add type="image/png"/>
        <add type="image/gif"/>
      </excludedMimeTypes>
      <excludedPaths>
        <add path="NoCompress.aspx"/>
      </excludedPaths>
    </httpCompress>
</blowery.web>

<add name="CompressionModule" type="blowery.Web.HttpCompress.HttpModule, blowery.web.HttpCompress"/>

有什么帮助吗?

4

1 回答 1

1

这是我之前遇到的一个问题,问题是 Content-Length 不正确。为什么不正确?因为它可能在压缩之前计算。

如果您手动设置 Content-Lenght,只需将其删除并让模块设置它,如果可以的话。

我注意到您使用Blowery 压缩。可能这是 Blowery 内部的错误/问题。如果找不到并修复它,为什么不使用 Ms 压缩?

@ptutt如果您在共享 iis 上,那么可能已经准备好设置压缩,所以有一个压缩比另一个压缩,您只需要删除您的压缩。如果这是问题所在,那么内容长度肯定是错误的,因为在第一次压缩之后,第二次会破坏它。

如果您的页面已默认由 iis 压缩,请使用此站点https://www.giftofspeed.com/gzip-test/进行检查。

如果默认情况下没有压缩,那么你可以很容易地做到这一点。在 Global.asax

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    string cTheFile = HttpContext.Current.Request.Path;
    string sExtentionOfThisFile = System.IO.Path.GetExtension(cTheFile);
    
    if (sExtentionOfThisFile.Equals(".aspx", StringComparison.InvariantCultureIgnoreCase))
    {
        string acceptEncoding = MyCurrentContent.Request.Headers["Accept-Encoding"].ToLower();;
        
        if (acceptEncoding.Contains("deflate") || acceptEncoding == "*")
        {
            // defalte
            HttpContext.Current.Response.Filter = new DeflateStream(prevUncompressedStream,
                CompressionMode.Compress);
            HttpContext.Current.Response.AppendHeader("Content-Encoding", "deflate");
        } else if (acceptEncoding.Contains("gzip"))
        {
            // gzip
            HttpContext.Current.Response.Filter = new GZipStream(prevUncompressedStream,
                CompressionMode.Compress);
            HttpContext.Current.Response.AppendHeader("Content-Encoding", "gzip");
        }       
    }
}

请注意,我只是写了这段代码,还没有测试过。我的代码有点复杂,所以我只创建了一个简单的版本。

查找更多示例: http ://www.google.com/search?q=Response.Filter+GZipStream

参考: 在负载平衡的服务器上加载时,ASP.NET 站点有时会冻结和/或在页面顶部显示奇怪的文本

于 2010-07-09T07:20:41.050 回答