Module to Gzip Compress dynamic aspx pages in Sharepoint

Hi Guys,

Recently we had a requirement to compress (gzip) all the dynamic aspx pages within our Sharepoint portal to be compressed before sent to the browser.

So this was the module I created to get the task done.


Enable Dynamic Page Compression in IIS then add the module in web.config



public class GZipCompressionModule : IHttpModule

    {


        public GZipCompressionModule()

        {


        }


        void IHttpModule.Dispose()

        {

        }


        void IHttpModule.Init(HttpApplication context)

        {

            context.BeginRequest += Context_BeginRequest;

            context.EndRequest += Context_EndRequest;



        }




        private void Context_BeginRequest(object sender, EventArgs e)

        {

            HttpApplication app = sender as HttpApplication;


            if (app.Request.RawUrl.ToLower().Contains(".aspx") &&

                app.Response.Filter.GetType() != typeof(GZipStream)

                )

            {


                app.Response.Filter = new GZipStream(app.Response.Filter,

                                                      CompressionMode.Compress);

            }



        }


        private void Context_EndRequest(object sender, EventArgs e)

        {

            HttpApplication app = sender as HttpApplication;


            if (app.Request.RawUrl.ToLower().Contains(".aspx"))

            {

                app.Response.AppendHeader("Content-Encoding", "gzip");

            }



        }


    }




Comments