Save your Bandwidth and make your web site performance better
While I work in my organization on service web site I faced problem that web site page some times take longer time that it expected espisaly in areas with slow internet connections and in same time very hight scale of serverbandwidth so I remebered that while I work with PHP that we used to gizp to commpress pages and make it smaller and easer to browse so what I did following, add to project Global.asax page and inside Application_BeginRequest event method add following code:
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
string acceptEncoding = app.Request.Headers["Accept-Encoding"];
Stream prevUncompressedStream = app.Response.Filter;
if (!string.IsNullOrEmpty(acceptEncoding) || (acceptEncoding.Length == 0))
return;
acceptEncoding = acceptEncoding.ToLower();
if (acceptEncoding.Contains(”gzip”))
{
app.Response.Filter = new GZipStream(prevUncompressedStream, CompressionMode.Compress);
app.Response.AppendHeader(”Content-Encoding”, “gzip”);
}
else if (acceptEncoding.Contains(”deflate”))
{
app.Response.Filter = new GZipStream(prevUncompressedStream, CompressionMode.Compress);
app.Response.AppendHeader(”Content-Encoding”, “deflate”);
}
}
but before that I add following name spaces :
<%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.IO.Compression" %>
Code work like following:
first we cast sender to HttpApplication because it is the object fired the event, and from this object we fetch header on (Accept-Encoding) attribute and look if browser support Gizp or Default then we pass uncompressed strem in compressed manner to save time and performance.
Hope it useful.

