我想调试为什么我的网页不是根据YSLOW GZIP'ed或放气的问题。我最终启用了服务器上的失败请求日志,并能够看到失败的原因,为什么它没有压缩,它认为它已经被压缩了。在IIS 8.5压缩不成功,说明ALREADY_CONTENT_ENCODING
DYNAMIC_COMPRESSION_NOT_SUCCESS Reason="ALREADY_CONTENT_ENCODING"
我已经在IIS中启用动态和静态压缩,我也改变了web.config文件以包含以下内容。
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="9" />
<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/json" enabled="true" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/atom+xml" enabled="true" />
<add mimeType="application/xaml+xml" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true"/>
另外我的aspx页面上我有我的每个页面(页面加载)之前调用一个方法来运行gzip压缩(这可能是为什么它是导致错误)。
这是我如何调用该方法从页面加载
//compress page
Compression.GZipEncodePage();
这是压缩页面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace InitialDataEntry
{
public static class Compression
{
/// <summary>
/// Sets up the current page or handler to use GZip through a Response.Filter
/// IMPORTANT:
/// You have to call this method before any output is generated!
/// </summary>
public static void GZipEncodePage()
{
HttpResponse Response = HttpContext.Current.Response;
if (IsGZipSupported())
{
string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
if (AcceptEncoding.Contains("deflate"))
{
Response.Filter = new System.IO.Compression.DeflateStream(Response.Filter,
System.IO.Compression.CompressionMode.Compress);
Response.AppendHeader("Content-Encoding", "deflate");
}
else
{
Response.Filter = new System.IO.Compression.GZipStream(Response.Filter,
System.IO.Compression.CompressionMode.Compress);
Response.AppendHeader("Content-Encoding", "gzip");
}
}
// Allow proxy servers to cache encoded and unencoded versions separately
Response.AppendHeader("Vary", "Content-Encoding");
}
/// <summary>
/// Determines if GZip is supported
/// </summary>
/// <returns></returns>
public static bool IsGZipSupported()
{
string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
string IsPartial = HttpContext.Current.Request.Headers["x-microsoftajax"];
if (!string.IsNullOrEmpty(AcceptEncoding) &&
AcceptEncoding.Contains("gzip") || AcceptEncoding.Contains("deflate"))
//Just checking to see if this is a partial page update
if (string.Compare("Delta=true", IsPartial, true) == 0)
{
return false;
}
else
{
return true;
}
else
{
return false;
}
}
}
}
此所用的方式进行工作的方法,但不知道什么时候它停了下来,但是我的用户意识到这个问题,因为页面的大小变得臃肿,以前的500k是2mb!
任何帮助将不胜感激。
感谢,
我没有验证的应用程序池使用集成模式下,除了我们有httpcompression还有urlcompression真正的静态和动态commpression。我试图运行一个演示站点,而不使用我们实现的gzip类,而且仍然没有压缩。 有趣的是,当我在本地服务器上运行网站时,我发现压缩能够正常工作,但只要有人试图从服务器外部看到它,它就不再被压缩。 – Eliseo