/// <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 / Больше-на-GZip-сжатии-с-ASPNET-контентом