Page compression enabling gzip and deflate HTTP compression in ASP. NET pages

Source: Internet
Author: User

Enabling gzip and deflate HTTP compression in ASP. NET pages
By Faisal Khan.

Re: http://www.stardeveloper.com/articles/display.html? Article = 2007110401 & page = 1

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 was ed 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 bandwi DTH (plus the bandwidth for headers sent and stored ed 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 schemes to as low as 12-15 kb, thus saving 25-28 KB of network bandwidth. now consider the implications of this network bandw Idth 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.

overview
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 shoshould know Is that HTTP compression is of two types, it can be gzip or deflate. gzip is the default. 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 isBeginrequest. The complete method signature to handle this event is displayed below:

 
Void application_beginrequest (Object sender, eventargs e ){}
Note: The naming convention used by ASP. NET to bind application events like Beginrequest 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 is Beginrequest .

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 "%>  

Explanation
Guys, the above Code isAllThat is required to enable HTTP compression for 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,GzipstreamAndDeflatestreamClasses are new in ASP. NET 2.0 and are present inSystem. Io. CompressionNamespace.

 
<% @ Import namespace = "system. Io" %> <% @ import namespace = "system. Io. Compression" %>

Next, we declare the method that will handleBeginrequestLife Cycle event. This event is the first event in the chain of events that take place when ASP. NET responds to a request.

 
Void application_beginrequest (Object sender, eventargs e ){...}

next, we cast the sender Object into a variable of Type httpapplication , because this is the object that fired this event to begin. 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 in 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 the prevuncompressedstream object. 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 = (httpapplication) sender; string acceptencoding = app. request. headers ["Accept-encoding"]; stream prevuncompressedstream = app. response. filter; If (acceptencoding = NULL | acceptencoding. length = 0) return;

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

 
Acceptencoding = acceptencoding. tolower ();

Finally, we searchAcceptencodingHeader for the "gzip" value. If present, we create a newGzipstreamObject and setRespone. FilterProperty to this value. This will mean that all the calltoResponse. Write ()In the ASP. NET code will actually accessGzipstreamObject, and the output will thus get compressed. next, we also set (or add) the outgoing Header "content-encoding" with the value of "gzip" to indicate to the client browser that the content encoding is going to be gzip compressed so that it will have to decompress it the client-side.

Lastly, if the client-browser does not support "gzip" stream but supports the "deflate" stream, we make useDeflatestreamObject in placeGzipstreamObject.

If (acceptencoding. contains ("gzip") {// gzipapp. response. filter = new gzipstream (prevuncompressedstream, compressionmode. compress); app. response. appendheader ("content-encoding", "gzip");} else if (acceptencoding. contains ("deflate") {// defalteapp. response. filter = new deflatestream (prevuncompressedstream, compressionmode. compress); app. response. appendheader ("content-encoding", "deflate ");}

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 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 stream if the client-browser supported it.

okay, this is it for now guys, take care and good bye.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.