How to enable GZip or Deflate on ASP.NET(HTTP Compression)

HTTP compression is a capability that can be built into web servers and web clients to make better use of available bandwidth, and provide greater transmission speeds between both.

HTTP data is compressed before it is sent from the server: compliant browsers will announce what methods are supported to the server before downloading the correct format; browsers that do not support compliant compression method will download uncompressed data. Compression as defined in RFC 2616 can be applied to the response data from the server (but not to the request data from the client)

The most common compression schemas include gzip and deflate.

So how do we gonna achieve this  HTTP Compression on asp.net application.?

Here is the Step :
  1. Open Global.asax file.
  2. Add the method IsCompressionAllowed() to check if compression is allowed.
  3. Add code in Application_Start event.

Global.asax File

void Application_Start(object sender, EventArgs e)
        {
            //check if request is aspx page.. if javascript etc ...that file cannot be compressed
            string sTheUrl = HttpContext.Current.Request.Url.LocalPath;
            string[] siTheUrl = sTheUrl.Split('?');
            string sUrl = siTheUrl[0].ToLower();
            if (sUrl.IndexOf(".aspx") <= 0)
            {
                goto End;
            }

            if (IsCompressionAllowed()) // If client browser supports HTTP compression  
            {
                //Browser supported encoding format     
                string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
                //use deflate first if deflate not available use gunzip since deflate more efficient (41% more faster than gzip)

                if (!string.IsNullOrEmpty(AcceptEncoding))
                {
                    if (AcceptEncoding.Contains("deflate"))
                    {
                        HttpContext.Current.Response.Filter = new DeflateStream(HttpContext.Current.Response.Filter, CompressionMode.Compress);
                        HttpContext.Current.Response.AppendHeader("Content-Encoding", "deflate");
                    }
                    else
                    {
                        HttpContext.Current.Response.Filter = new GZipStream(HttpContext.Current.Response.Filter, CompressionMode.Compress);
                        HttpContext.Current.Response.AppendHeader("Content-Encoding", "gzip");
                    }
                }
            }

            // Allow proxy servers to cache encoded and unencoded versions separately 
            //Response.AppendHeader("Vary", "Content-Encoding");
            HttpContext.Current.Response.AppendHeader("Vary", "Content-Encoding");

        End:
            char hello;

        }

        private bool IsCompressionAllowed()
        {
            //Browser supported encoding format  
            string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
            if (!string.IsNullOrEmpty(AcceptEncoding))
            {
                if (AcceptEncoding.Contains("gzip") || AcceptEncoding.Contains("deflate"))
                    return true; ////If browser supports encoding, is either one
            } 
return false;
        }


By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

Popular posts from this blog

Example to disable save as certain file type in SSRS Report Viewer

How to create DataGrid or GridView in JSP - Servlet

Control Webpart Visible/Enable using macro in Kentico