Adding on the performance tips for your asp.net site, using gzip compression for request and response can improve the network usage by more then 90%

 

Following is the step to do that:

First you need to add a httpmodule to your web application. Following is the code

 

namespace SecureFN.Common.Compression

{

 

    public class CompressionModule : IHttpModule

    {

        public void Dispose()

        {

        }

 

        public void Init(HttpApplication app)

        {

            app.PreRequestHandlerExecute += new EventHandler(Compress);

        }

 

        private void Compress(object sender, EventArgs e)

        {

            HttpApplication app = (HttpApplication)sender;

            HttpRequest request = app.Request;

            HttpResponse response = app.Response;

 

            //Ajax Web Service request is always starts with application/json

            if (request.ContentType.ToLower(CultureInfo.InvariantCulture).StartsWith("application/json"))

            {

                //User may be using an older version of IE which does not support compression, so skip those

                if (!((request.Browser.IsBrowser("IE")) && (request.Browser.MajorVersion <= 6)))

                {

                    string acceptEncoding = request.Headers["Accept-Encoding"];

 

                    if (!string.IsNullOrEmpty(acceptEncoding))

                    {

                        acceptEncoding = acceptEncoding.ToLower(CultureInfo.InvariantCulture);

 

                        if (acceptEncoding.Contains("gzip"))

                        {

                            response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);

                            response.AddHeader("Content-encoding", "gzip");

                        }

                        else if (acceptEncoding.Contains("deflate"))

                        {

                            response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);

                            response.AddHeader("Content-encoding", "deflate");

                        }

                    }

                }

            }

        }

    }

}

 

Second step is to add the entry in web.config

For IIS6 add the handler to httpModules, for IIS7 add modules

<add name="CompressionModule" type="Compression.SecureFNCompressionModule"/>

 

Next and last step is to add this to your requests. For example, while using jquery post, add following:

        contentType: "application/json;charset=utf-8",

 dataType: "json",

        processdata: true,

        beforeSend: function (request) {

            request.setRequestHeader("Accept-Encoding", "gzip");

        }