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
Go to top

Minimizing VIEWSTATE Size and Customization of Server Reponse Via HTTPMODULE

, 19 Oct 2010
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.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.IO;
using System.IO.Compression;

namespace SaXXXoN.Controls.Web
{
    public class ZipPageStatePersister : PageStatePersister
    {
        public ZipPageStatePersister(Page _page) : base(_page) { }

        #region Load Method
        /// <summary>
        /// Load State Information when Client will be make  POSTBACK request
        /// </summary>
        public override void Load()
        {
            if (!string.IsNullOrEmpty(this.Page.Request.Form["_VSTATE"]))
            {
                byte[] bytes = Convert.FromBase64String(this.Page.Request.Form["_VSTATE"]);

                byte[] _decompressed = Decompress(bytes);

                StringReader _reader = new StringReader(Convert.ToBase64String(_decompressed));

                LosFormatter _formatter = new LosFormatter();
                Pair _pair = _formatter.Deserialize(_reader.ReadToEnd()) as Pair;

                if (_pair != null)
                {
                    this.ViewState = _pair.First;
                    this.ControlState = _pair.Second;
                }
            }
        }
        #endregion

        #region Save Method
        /// <summary>
        /// Save Method will Compress and save state information in Persistence Medium
        /// </summary>
        public override void Save()
        {
            if (this.ViewState != null || this.ControlState != null)
            {
                using (StringWriter _writer = new StringWriter())
                {
                    Pair _pair = new Pair(this.ViewState, this.ControlState);

                    LosFormatter _formatter = new LosFormatter();
                    _formatter.Serialize(_writer, _pair);

                    string _zippedData = CompressData(Convert.FromBase64String(_writer.ToString()));

                    this.Page.ClientScript.RegisterHiddenField("_VSTATE", _zippedData);
                }
            }
        }
        #endregion

        #region Compress Data
        /// <summary>
        /// Compress Data
        /// </summary>
        /// <param name="data"></param>
        /// <returns>Base64 Encoded Result</returns>
        public static string CompressData(byte[] data)
        {
            using (MemoryStream mStream = new MemoryStream())
            {
                GZipStream zipper = new GZipStream(mStream, CompressionMode.Compress);

                zipper.Write(data, 0, data.Length);
                zipper.Close();

                return Convert.ToBase64String(mStream.ToArray());
            }
        }
        #endregion

        #region DeCompress Data
        /// <summary>
        /// Decompress Data
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] Decompress(byte[] data)
        {
            using (MemoryStream input = new MemoryStream())
            {
                input.Write(data, 0, data.Length);
                input.Position = 0;
                GZipStream gzip = new GZipStream(input,
                                  CompressionMode.Decompress, true);
                MemoryStream output = new MemoryStream();
                byte[] buff = new byte[64];
                int read = -1;
                read = gzip.Read(buff, 0, buff.Length);
                while (read > 0)
                {
                    output.Write(buff, 0, read);
                    read = gzip.Read(buff, 0, buff.Length);
                }
                gzip.Close();
                return output.ToArray();
            }
        }
        #endregion
    }


}

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 | Mobile
Web02 | 2.8.140916.1 | Last Updated 19 Oct 2010
Article Copyright 2010 by Muhammad Shoaib Raja
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid