If the response time of your website is high, i.e if it is taking more time to display the content then the user might lose the interest in using the website. So it is important the response should be as fast as possible.
Compression is one of the methods in which we can reduce the size of the response. It reduces the response time by reducing the size of HTTP response.
Basically there are two types of compression, i.e Gzip and Deflate.
Gzip is the most popular and effective compression method at this time. It was developed by the GNU project and standardized by RFC 1952. The only other compression format you’re likely to see is deflate, but it’s less effective and less popular.
Then next question that will come to our mind is “How to compress your files in ASP.NET?”.
It includes the following steps:
Step 1: Check whether browser support compression by checking Accept-Encoding header in HTTP request.
Accept-Encoding: gzip, deflate.
Step 2: Compress the response at server side using any one of the compression techniques.
Step 3: Server sends the compressed response and indicates the browser by setting Content-Encoding header in the response.
Content-Encoding: gzip
Now the question is “What to gzip and what not to?”. Mostly we can gzip HTML documents, along with gzipping HTML documents we can also gzip stylesheets and scritps. Images should not be gzipped, since they already are comressed. So it is unnecessary to gzip them. Code snippet for gzipping: At PostRelaseRequestState event, the whole page has executed and its response content has been generated. We will compress the response in that event.
The following code snippet demonstrates how to achieve that.
public class GlobalGZip : System.Web.HttpApplication { public GlobalGZip() { InitializeComponent(); } private void InitializeComponent() { this.PostReleaseRequestState += new EventHandler(Global_PostReleaseRequestState); } /// /// Event handler for PostReleaseRequestState /// private void Global_PostReleaseRequestState(object sender, EventArgs e) { string contentType = Response.ContentType; // Get the content type. // Compress only html and stylesheet documents. if (contentType == "text/html" || contentType == "text/css") { // Get the Accept-Encoding header value to know whether zipping is supported by the browser or not. string acceptEncoding = Request.Headers["Accept-Encoding"]; if (!string.IsNullOrEmpty(acceptEncoding)) { // If gzip is supported then gzip it else if deflate compression is supported then compress in that technique. if (acceptEncoding.Contains("gzip")) { // Compress and set Content-Encoding header for the browser to indicate that the document is zipped. Response.Filter = new GZipStream(Response.Filter, CompressionMode.Compress); Response.AppendHeader("Content-Encoding", "gzip"); } else if (acceptEncoding.Contains("deflate")) { // Compress and set Content-Encoding header for the browser to indicate that the document is zipped. Response.Filter = new DeflateStream(Response.Filter, CompressionMode.Compress); Response.AppendHeader("Content-Encoding", "deflate"); } } } } }
The next step is to use the above class. We can use this class in Global.asax file which contains application level events.
Hope this helps you.
Thanks : )