Enabling Gzip and Deflate HTTP Compression in ASP.NET pages

By: Jimit Shah | Views: 3732 | Date: 18-Jun-2010

The output of most ASP.NET pages is in the form of text. In the terms of HTTP, the content-type is called "text/html". The text received by the client browser, from the ASP.NET page on an IIS Web Server, is then rendered on your computer screen. Since this communication takes place over the internet, it consumes bandwidth.

The output of most ASP.NET pages is in the form of text. In the terms of HTTP, the content-type is called "text/html". The text received by the client browser, from the ASP.NET page on an IIS Web Server, is then rendered on your computer screen. Since this communication takes place over the internet, it consumes bandwidth. For instance, a 40 KB ASP.NET page will take exactly 40 KB of network bandwidth (plus the bandwidth for headers sent and received by the client browser). To optimize this communication cycle, HTTP Gzip and Deflate compression is used. HTTP compression will enable a 40 KB output from an ASP.NET page to be compressed using Gzip or Deflate compression to as low as 12-15 KB, thus saving 25-28 KB of network bandwidth. Now consider the implications of this network bandwidth savings over hundreds of thousands of requests handled by IIS Server each day. This can be huge? Isn't it? Add to this the fact that to the users accessing your website, your website will load faster and will thus improve the user experience and perception of your website.

How to enable HTTP Compression in our ASP.NET pages?
Now that you understand, how important HTTP compression can be for your website and its visitors, we will discuss what is required to achieve HTTP Gzip and Deflate compression in our ASP.NET websites. The first thing that you should know is that HTTP compression is of two types, it can be Gzip or Deflate. To use HTTP compression in our ASP.NET pages without editing every single one of them, we will make use of ASP.NET web application life cycle events in Global.asax file.

A Global.asax file is placed in your ASP.NET application folder. It can contain methods to handle ASP.NET web application life cycle events. The names and details of all web application events that we can handle in Global.asax file is beyond the scope of this tutorial. But one event that we will handle and where we will insert our code isPreRequestHandlerExecute. It occurs just before ASP.NET starts executing an event handler. The complete method signature to handle this event is displayed below:

void Application_PreRequestHandlerExecute(object sender, EventArgs e){	...}
Note: The naming convention used by ASP.NET to bind application events likePreRequestHandlerExecute with actual event handlers is to use the name of object who fires the event, in this case Application, with the underscore (_) character and then finally comes the event name itself, which in our case isPreRequestHandlerExecute.

Why our default HTTP Compression Method will be Deflate?
We will be using Deflate HTTP compression by default because it offers faster compression and decompression than Gzip.

Global.asax

Ok folks, let's cut the details and go straight to the code. If your ASP.NET web application doesn't already contain a Global.asax file, create a new one using notepad. Then insert following code in it:

<%@ Application Language="C#" %><%@ Import Namespace="System.IO" %><%@ Import Namespace="System.IO.Compression" %><script runat="server">void Application_PreRequestHandlerExecute(object sender, EventArgs e){	HttpApplication app = sender as HttpApplication;	string acceptEncoding = app.Request.Headers["Accept-Encoding"];	Stream prevUncompressedStream = app.Response.Filter;	if (!(app.Context.CurrentHandler is Page ||		app.Context.CurrentHandler.GetType().Name == "SyncSessionlessHandler") || app.Request["HTTP_X_MICROSOFTAJAX"] != null)		return;	if (acceptEncoding == null || acceptEncoding.Length == 0)		return;	acceptEncoding = acceptEncoding.ToLower();	if (acceptEncoding.Contains("deflate") || acceptEncoding == "*")	{		// defalte		app.Response.Filter = new DeflateStream(prevUncompressedStream,			CompressionMode.Compress);		app.Response.AppendHeader("Content-Encoding", "deflate");	}	else if(acceptEncoding.Contains("gzip"))	{		// gzip		app.Response.Filter = new GZipStream(prevUncompressedStream,			CompressionMode.Compress);		app.Response.AppendHeader("Content-Encoding", "gzip");	}}</script>

Explanation
Guys, the above code is all that is required to enable HTTP compression in your ASP.NET pages. This is true, pretty simple stuff. Let us now focus on the details of the code presented above.

The first thing we do is to tell the ASP.NET compiler that the forthcoming code is going to be in the language, C#.

<%@ Application Language="C#" %>

Next, we import the namespaces that our code will make use of. Remember, the GZipStreamand DeflateStream classes are new in ASP.NET 2.0 and are present in theSystem.IO.Compression namespace.

<%@ Import Namespace="System.IO" %><%@ Import Namespace="System.IO.Compression" %>

Next, we declare the method that will handle the PreRequestHandlerExecute life cycle event. As explained earlier, this event occurs just before ASP.NET starts executing an event handler.

void Application_PreRequestHandlerExecute(object sender, EventArgs e){	...}

Next, we properly cast the sender Object, using the as keyword, into a variable of typeHttpApplication, because this is the object that fired this event to begin with. Using this object, we fetch the headers collection sent by the client-browser. Specifically, we are interested in a "Accept-Encoding" header. If a client browser supports compressed content streams (which most browsers do) of the types, Gzip and Deflate, the value of this header will contain them. A reference to current uncompressed output Stream object used by ASP.NET to send the plain text (or binary) output to the client browser is saved in theprevUncompressedStream object.

We try to make sure that the requested ASP.NET resource is either a valid ASP.NET page or a Web Service. If it is not, then we return and do nothing. Additionally, we also check for "HTTP_X_MICROSOFTAJAX" item in the HttpRequest collection. This item is often present when using AJAX in ASP.NET pages. When the ASP.NET content is compressed when AJAX is being used, the page can throw exceptions. So we play safe, and don't compress the page if AJAX is being used.

Note: You can now enable HTTP Compression not only in your ASP.NET web pages but in your ASP.NET web services as well. All you have to do is to also check for the name of the type of CurrentHandler property and match it with "SyncSessionlessHandler". It will only match if it is a Web Service. And that's it.

Thanks Scott Gartner!

Finally, we check to see if either the client browser did not send the "Accept-Encoding" header, or if it did send, but its value is empty, will mean that client-browser cannot process compressed content. In that case, we simply return from this method and do nothing

	HttpApplication app = sender as HttpApplication;	string acceptEncoding = app.Request.Headers["Accept-Encoding"];	Stream prevUncompressedStream = app.Response.Filter;		if (!(app.Context.CurrentHandler is Page ||		app.Context.CurrentHandler.GetType().Name == "SyncSessionlessHandler") ||		app.Request["HTTP_X_MICROSOFTAJAX"] != null)		return;	if (acceptEncoding == null || acceptEncoding.Length == 0)		return;

We then set the case of all the characters in the acceptEncoding header's value to lower case. We do this so that when we attempt to compare the presence of strings like "gzip" and "deflate" in the acceptEncoding variable, values like "GZIP", "gZiP", and "Gzip" will be interpreted as one and the same.

	acceptEncoding = acceptEncoding.ToLower();

Finally, we search the acceptEncoding header for the "deflate" value. If present, we create a new DeflateStream object and set the Respone.Filter property to this value. This will mean that all the calls to Response.Write() in the ASP.NET code will actually access theDeflateStream object, and the output will thus get compressed. Next, we also set (or add) the outgoing header "Content-Encoding" with the value of "deflate" to indicate to the client browser that the content encoding is going to be Deflate compressed so that it will have to decompress it at the client-side.

Lastly, if the client-browser does not support "deflate" stream but supports the "gzip" stream, we make use of GZipStream object in place of DeflateStream object.

if (acceptEncoding.Contains("deflate") || acceptEncoding == "*"){	// defalte	app.Response.Filter = new DeflateStream(prevUncompressedStream,		CompressionMode.Compress);	app.Response.AppendHeader("Content-Encoding", "deflate");}else if (acceptEncoding.Contains("gzip")){	// gzip	app.Response.Filter = new GZipStream(prevUncompressedStream,		CompressionMode.Compress);	app.Response.AppendHeader("Content-Encoding", "gzip");}

That's all guys. Now make the changes described above and access an ASP.NET page in your website. This time the page will be Deflate HTTP compressed and will load quickly.

Summary

We learned a very important technique in this tutorial to use HTTP compression in our ASP.NET pages. To do that, the procedure turned out to be simpler than we anticipated. We made use of a single event in the Global.asax file to switch stream for our ASP.NET pages to a compressed HTTP stream using Deflate or Gzip, if the client-browser supported it. You can learn more about Gzip and Deflate HTTP compression and speed comparison by reading that article.

Previous Page Next Page

People Searching On This Page:
  • ricardoulrich@netuno.com.br loc:IN
  • asp.net using compressed gzip on pages'
  • gzip global.asax Application_PreRequestHandlerExecute
  • net 4.0 web services compression
  • info gaip.net loc:IN
  • enable gzip compression in asp.et
  • Application_PreRequestHandlerExecute gzip
  • gaip.net loc:IN
  • IIS6 how to enable gzip compression
  • asp.net Make sure HTTP compression is turned on for
  • asp.net compression
  • life cycle Application_PreRequestHandlerExecute
  • enable deflate compression with asp.net 4.0
  • deflate compression
  • asp enable deflate compression

Related Pages


Penetration testing services – Know how safe your network is

Penetration testing services – Know how safe your network is

Articles | Computers | Security
Date:
23-Aug-2011  Views: 1121

Penetration testing services is becoming one of the popular ways of assessing whether the network of an organization is strong or not. Penetration tes ...
Interview Questions on ASP.Net 4.0 Features

Interview Questions on ASP.Net 4.0 Features

Articles | Programming | ASP.NET
Date:
17-Aug-2011  Views: 2066

This section provides all possible ASP.Net 4.0 interview questions collected from various people from their experiences and various resources. Please ...
ASP.NET 4.0 Features - What is New ?

ASP.NET 4.0 Features - What is New ?

Articles | Programming | ASP.NET
Date:
12-Aug-2011  Views: 2059

ASP.NET v4 is released with Visual studio 2010. Web developers are presented with a bewildering range of new features and so Ludmal De Silva has descr ...
ASP.NET Interview Questions And Answers

ASP.NET Interview Questions And Answers

Articles | Programming | ASP.NET
Date:
25-Mar-2011  Views: 2627

Collection of ASP.NET 3.5 Interview Questions & Answers which are being asked in most interview. ...
Interview Questions for ASP.NET Part - 1

Interview Questions for ASP.NET Part - 1

Articles | Programming | ASP.NET
Date:
28-Aug-2010  Views: 1647

You can read more than 60 ASP.NET Interview Questions. ...
Post Your Comments (No Login Require)
Name : (required)
Email : (required)
Website :

Comment : (required)

26  + 8 =     
Comments
People Searched About:
Ricardoulrich@Netuno.Com.Br Loc:IN   |   Asp.Net Using Compressed Gzip On Pages'   |   Gzip Global.Asax Application_Prerequesthandlerexecute   |   Net 4.0 Web Services Compression   |   Info Gaip.Net Loc:IN   |   Enable Gzip Compression In Asp.Et   |   Application_Prerequesthandlerexecute Gzip   |   Gaip.Net Loc:IN   |   Life Cycle Application_Prerequesthandlerexecute   |   Deflate Compression   |   Asp Enable Deflate Compression   |   Asp.Net Gzip Add File   |   Gzip And Deflate Compression Asp.Net   |   Asp.Net Enable Deflate   |   Http Compression Prerequesthandlerexecute   |   Gzip Compression Global File Asp.Net   |  
Google : 515 times | Yahoo : 155 times | Bing : 497 times |