有没有可能压缩一个asp.net 3.5页面?我的站点托管在IIS7上,由于技术原因,我不能在站点范围内启用gzip压缩。IIS7是否可以选择对单个页面进行压缩,或者我是否必须覆盖OnPreRender并编写一些代码来压缩输出?
发布于 2011-08-04 18:00:10
您可以使用Blowery HttpCompress module。在web.config中,您可以指定要从压缩中排除的文件。
<httpCompress preferredAlgorithm="gzip" compressionLevel="high">
<excludedMimeTypes>
<add type="application/pdf"/>
</excludedMimeTypes>
<excludedPaths>
<add path="/pathToExclude"/>
<add path="WebResource.axd"/>
<add path="ScriptResource.axd"/>
</excludedPaths>
</httpCompress>发布于 2011-08-04 18:19:04
/// <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"];
if (!string.IsNullOrEmpty(acceptEncoding) &&
(acceptEncoding.Contains("gzip") || acceptEncoding.Contains("deflate")))
{
return true;
}
return false;
}我在一个名为CompressionUtilities的类中有这段代码。然后在页面中,你想要GZIP (或者在我的例子中,我想要GZIP的所有页面的共享基页)
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
CompressionUtilities.GZipEncodePage();
}来源:http://www.west-wind.com/weblog/posts/2007/Feb/05/More-on-GZip-compression-with-ASPNET-Content
https://stackoverflow.com/questions/6939304
复制相似问题