Increase ASP Dot Net Website Loading Speed Using Web GZip Compression
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
Sadly a large number ASP.Net web applications on the internet often do not use HTTP GZIP compression.
Most modern browsers support GZIP compression. The benefits of compression are huge essentially significantly reducing the size of the HTML content that needs to be transfered. The end result is that the page loads in the browser in a lot less time than otherwise.
In general if using ViewState on average most pages can be compressed to half their original size. This means that your application page will be delivered to the browser within half the time.
If not using ViewState most pages will compress to almost one third of their original size or smaller. This means that your application page will be delivered to the browser in one third of the time.
Not only does it speed up your application it also reduces your bandwidth consumption.
How To Implement:
One method of course is to try and enable IIS GZIP compression. However what I’ve found is that even when you manage to finally get it working the performance is not as good as it could be.
On the other hand you could easily write your own GZIP compressor as an http handler.
The following code sample does exactly that.
Firstly things first the disclaimer:
Disclaimer:
This code is provided free of cost as is and where is with no warranty of fitness for any purpose whatsoever either express or implied including but not limited to any implied warranties of merchantability and fitness for any particular purpose.
Use entirely at your own risk.
Dependencies:
The GZIP compression library ICSharpCode.SharpZipLib is a very good GPL licensed GZIP compressor. Therefore this solution uses this library for GZIP compression. Please check their license terms first before using any of this.
The http handler code itself is free to use as you wish as long as you link back to me requested below.
Link Back to me:
If you end up using this code all I ask is that you provide a link back to either:
http://www.chillx.com
or to
http://www.tcwicks.com
Compiled Binaries:
The following zip file contians the compiled binaries.
tcwicks_dot_com_http_gzip_compression_for_asp_dot_net.zip
Usage:
1) Uncompress the zip file and drop the dll files into your web applications bin folder. Alternatively add the two files as binary file references to your web application visual studio project.
2) Add the following to your web.config file inside the System.Web section
<httpHandlers>
<add verb=”*” path=”*.aspx” type=”TCWCompress.tcwhttphandler,TCWHTTPCompress”/>
</httpHandlers>
And that is it – all done. To test compression you can use firefox developer tools or a site like SeoConsultants
p.s. By adjusting the path parameter you can change what gets compressed. for example “/MyBigPages/*.aspx” will compress only the output from application pages inside the /MyBigPages subfolder.
Setting up Compress everything except specific pages:
Certain parts of your application such as some ajax handler pages may not play well with compression. If that is the case and you want to compress everything except a few specific pages than you can us the strategy of compress everything except those specified.
Say for example if you want to compress all pages except /ajaxtextboxhandler.aspx and /otherajax/*.aspx
The following http handlers setup in your web.config will do this for you
<httpHandlers>
<add verb=”*” path=”/ajaxtextboxhandler.aspx” type=”System.Web.UI.PageHandlerFactory”/>
<add verb=”*” path=”*/otherajax/*.aspx” type=”System.Web.UI.PageHandlerFactory”/>
<add verb=”*” path=”*.aspx” type=”TCWCompress.tcwhttphandler,TCWHTTPCompress”/>
</httpHandlers>
Source Code:
The source code is listed below just in case you would like to modify the behavior of the compressor. If not you can ignore this source code and just place the compiled dll in your bin folder.
The following is the C# source code used for the gzip compression http handler.
p.s. You might want to modify the contents of the SendErrorResponse method to redirect to a nice 404 page instead. HttpContext.Current.Response.Redirect will work just the same way as Response.Redirect works from within a page..
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.SessionState;
namespace TCWCompress
{
public class tcwhttphandler : IHttpHandler, IRequiresSessionState
{
public tcwhttphandler()
{
}
#region IHttpHandler Members
public bool IsReusable
{
get
{
return true;
}
}
public void ProcessRequest(HttpContext context)
{
string request_context;
IHttpHandler handler;
request_context = string.Concat(context.Request.ServerVariables.GetValues(“URL”)[0], context.Request.PathInfo);
try
{
if (IsGZipSupported(context))
{
context.Response.AppendHeader(“Content-Encoding”, “gzip”);
ICSharpCode.SharpZipLib.GZip.GZipOutputStream OutputGZIPStream;
OutputGZIPStream = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(context.Response.Filter);
OutputGZIPStream.SetLevel(ICSharpCode.SharpZipLib.Zip.Compression.Deflater.BEST_COMPRESSION);
context.Response.Filter = OutputGZIPStream;
}
handler = PageParser.GetCompiledPageInstance(request_context, null, context);
if (handler == null)
{
SendErrorResponse(context);
}
else
{
handler.ProcessRequest(context);
}
}
catch (Exception ex)
{
bool ThreadAbort;
ThreadAbort = (ex is System.Threading.ThreadAbortException);
if (!ThreadAbort)
{
throw (ex);
}
}
}
public static bool IsGZipSupported(HttpContext context)
{
string AcceptEncoding = context.Request.Headers["Accept-Encoding"];
if (!string.IsNullOrEmpty(AcceptEncoding))
{
AcceptEncoding = AcceptEncoding.ToLower();
if (AcceptEncoding.Contains(“gzip”) || AcceptEncoding.Contains(“deflate”))
{
return true;
}
}
return false;
}
protected virtual void SendErrorResponse(HttpContext context)
{
HttpContext Context = HttpContext.Current;
context.Response.StatusCode = 404;
context.Response.StatusDescription = “Invalid Web Resource”;
context.Response.Write(“HTTP Error 404 – File or directory not found.”);
context.Response.End();
}
#endregion
}
}

February 28th, 2009 at 5:56 pm
thanks !! very helpful post!
May 7th, 2009 at 7:15 am
where to put the above source code after adding in bin folder and Web.Congif file
July 6th, 2009 at 7:18 pm
Hi! I like your srticle and I would like very much to read some more information on this issue. Will you post some more?
January 27th, 2010 at 8:38 pm
Not working with css and js file?
May 1st, 2010 at 1:31 pm
I can’t imagine not liking this! can’t wait to try this, I post many of your recipes on my blog, I so love my crockpot!
June 27th, 2010 at 10:49 am
Hi admin How are You ? I like your post and i want to stumble it for my friend but i cant see your social bookmark widget in this blog. Please help me friend Thank You