To enable Gzip and deflate compression in Asp.net website, you just need to place this code in global.asax.
however you can also apply this setting to IIS level.
void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
try
{
HttpResponse Response = HttpContext.Current.Response;
String AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
if (String.IsNullOrEmpty(AcceptEncoding) && AcceptEncoding.Contains("gzip") || AcceptEncoding.Contains("deflate"))
{
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");
}
}
Response.AppendHeader("Vary", "Content-Encoding");
}
catch (Exception)
{
}
}
however you can also apply this setting to IIS level.
void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
try
{
HttpResponse Response = HttpContext.Current.Response;
String AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
if (String.IsNullOrEmpty(AcceptEncoding) && AcceptEncoding.Contains("gzip") || AcceptEncoding.Contains("deflate"))
{
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");
}
}
Response.AppendHeader("Vary", "Content-Encoding");
}
catch (Exception)
{
}
}
Nice code snippet...grt
ReplyDeletethanks buddy...this is just a beginning...Follow this site for more code.
ReplyDelete