Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version

Minimizing VIEWSTATE Size and Customization of Server Reponse Via HTTPMODULE

, 19 Oct 2010 CPOL
VIEWSTATE is one of the primary factors which affect your web application. In this article, I will try to highlight the way to compress VIEWSTATE and customize server response using HTTPMODULES
Code.zip
Code
PageStatePersister
bin
Debug
UtilControlsCollection.dll
icon.ico
Properties
WebApplication1
WebApplication1
App_Data
bin
UtilControlsCollection.dll
UtilControlsCollection.pdb
WebApplication1.dll
WebApplication1.pdb
Properties
WebApplication1.csproj.user
WebApplication1.suo
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text.RegularExpressions;

namespace WebApplication1
{
    public class StdViewStateRemover : System.Web.IHttpModule
    {
        private System.Web.HttpApplication mApplication;

        public StdViewStateRemover()
        {
        }

        public void Init(System.Web.HttpApplication application)
        {
            // Wire up beginrequest
            application.BeginRequest += new System.EventHandler(BeginRequest);
            // Save the application
            mApplication = application;
        }

        public void BeginRequest(Object sender, EventArgs e)
        {
            //for testing purposes i just checking jpg images
            if (((HttpApplication)sender).Request.Path.ToLower().Contains("jpg".ToLower()))
            {
                return;
            }

            // Create a new filter
            VSStream mStreamFilter = new VSStream(mApplication.Response.Filter);
            // Insert it onto the page
            mApplication.Response.Filter = mStreamFilter;
        }

        // ViewState filter
        public class VSStream : System.IO.Stream
        {
            //  The stream to the client
            private System.IO.Stream moStream;
            // Used to track properties not supported by the client stream
            private long mlLength;
            private long mlPosition;
            // An easy way to write a stream to the client
            private System.IO.StreamWriter mSR;

            public VSStream(System.IO.Stream stream)
            {
                // Save of the stream back to the client
                moStream = stream;
                // Create a streamwriter for later use
                mSR = new System.IO.StreamWriter(moStream);
            }

            public override bool CanRead
            {
                get
                {
                    return false;
                }
            }

            public override bool CanSeek
            {
                get
                {
                    return true;
                }
            }

            public override bool CanWrite
            {
                get
                {
                    return true;
                }
            }

            public override long Length
            {
                get
                {
                    return mlLength;
                }
            }

            public override long Position
            {
                get
                {
                    return mlPosition;
                }
                set
                {
                    mlPosition = value;
                }
            }

            public override int Read(Byte[] buffer, int offset, int count)
            {
                throw new NotSupportedException();
            }

            public override long Seek(long offset, System.IO.SeekOrigin direction)
            {
                return moStream.Seek(offset, direction);
            }

            public override void SetLength(long length)
            {
                mlLength = length;
            }

            public override void Close()
            {
                moStream.Close();
            }

            public override void Flush()
            {
                moStream.Flush();
            }

            public override void Write(byte[] buffer, int offset, int count)
            {
                System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding();
                System.Text.StringBuilder strBuff = new System.Text.StringBuilder(utf8.GetString(buffer));

                //Regular Expression to Find ViewState in Response
                Regex reg = new Regex("<input type=\"hidden\" name=\"__VIEWSTATE\"[\\w\\W]*/>");

                string _temp = reg.Replace(strBuff.ToString(), string.Empty);
                strBuff.Remove(0, strBuff.Length - 1).Append(_temp);

                mSR.Write(strBuff.ToString());
            }
        }

        public void Dispose()
        {
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Muhammad Shoaib Raja
Software Developer Pakistan Revenu Automation Ltd. (PRAL)
Pakistan Pakistan
I am passionate web developer and mostly works in ASP.NET and Adobe Flex applications. Now my keen of learning is driving me toward Node.js and believe me, i truly love it.
 
I have just started my blog and you can find it Here

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 19 Oct 2010
Article Copyright 2010 by Muhammad Shoaib Raja
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid