Compressing ASP.NET 2.0 Viewstate

One thing I was looking forward with asp.net 2.0 release was improved viewstate optimization. The truth is it has improved, but for me there is still something desirable to be had.

Where I have run into the problems is a web application I am working on the must allow the user to add an infinate amount of web controls dynamically. The results below are pretty amazing. I ended up using SharpZipLib for the compressing algorithm.

Test Scenerio: One web page with that ended up with 116 asp.net web controls add to it.

Result of ViewState Size:
Before Compression: 43.3 KB
After Compression using SharpZipLib: 3.50 KB

Below is the code to get you started

Imports System.IO

Imports Zip = ICSharpCode.SharpZipLib.Zip.Compression

 

Public Class PageViewStateZip : Inherits System.Web.UI.Page

 

Protected Overrides Function LoadPageStateFromPersistenceMedium() As Object

Dim vState As String = Me.Request.Form(“__VSTATE”)

Dim bytes As Byte() = System.Convert.FromBase64String(vState)

bytes = vioZip.Decompress(bytes)

Dim format As New LosFormatter

 

Return format.Deserialize(System.Convert.ToBase64String(bytes))

 

End Function

 

Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal viewState As Object)

Dim format As New LosFormatter

Dim writer As New StringWriter

format.Serialize(writer, viewState)

Dim viewStateStr As String = writer.ToString()

Dim bytes As Byte() = System.Convert.FromBase64String(viewStateStr)

bytes = vioZip.Compress(bytes)

Dim vStateStr As String = System.Convert.ToBase64String(bytes)

RegisterHiddenField(“__VSTATE”, vStateStr)

 

End Sub

 

End Class

 

 

Public Class vioZip

Shared Function Compress(ByVal bytes() As Byte) As Byte()

Dim memory As New MemoryStream

Dim stream = New Zip.Streams.DeflaterOutputStream(memory, _

New Zip.Deflater(Zip.Deflater.BEST_COMPRESSION), 131072)

stream.Write(bytes, 0, bytes.Length)

stream.Close()

Return memory.ToArray()

End Function

 

Shared Function Decompress(ByVal bytes() As Byte) As Byte()

Dim stream = New Zip.Streams.InflaterInputStream(New MemoryStream(bytes))

Dim memory As New MemoryStream

Dim writeData(4096) As Byte

Dim size As Integer

While True

size = stream.Read(writeData, 0, writeData.Length)

 

If size > 0 Then memory.Write(writeData, 0, size) Else Exit While

End While

stream.Close()

Return memory.ToArray()

End Function

 

End Class

Tim

A .NET, PHP, Marketing Guru authority, at least I hope I am. Reach me at tboland@gmail.com

42 thoughts on “Compressing ASP.NET 2.0 Viewstate

  1. Clenbuterol 40mcg T3/T4 Cytomel Special Offer Ketotifen clenbuterol, buy clenbuterol, clenbuterol weight loss, clenbuterol uk, order clenbuterol, cytomel t3, liquid clenbuterol, clenbuterol for sale, clenbuterol hydrochloride, clenbuterol side effects, clenbuterol side effects

    http://www.clenbuterol.co.uk

  2. Wow!!! Its really works!!! Thank you guys!!!

    Here, my c# translation:

    using System.IO;
    using Zip = ICSharpCode.SharpZipLib.Zip.Compression;

    public class BasePage: System.Web.UI.Page
    {

    protected override object LoadPageStateFromPersistenceMedium()
    {

    string _viewState = this.Request.Form[“__VSTATE”];
    byte[] _bytes = System.Convert.FromBase64String(_viewState);

    _bytes = CompressToZip.Decompress(_bytes);

    LosFormatter _format = new LosFormatter();
    String _newViewState = Convert.ToBase64String(_bytes);

    return _format.Deserialize(_newViewState);
    }

    protected override void SavePageStateToPersistenceMedium(object state)
    {
    LosFormatter _format = new LosFormatter();
    StringWriter _writer = new StringWriter();

    _format.Serialize(_writer, state);

    String _viewStateString = _writer.ToString();

    byte[] _bytes = System.Convert.FromBase64String(_viewStateString);
    _bytes = CompressToZip.Compress(_bytes);

    String _newViewState = Convert.ToBase64String(_bytes);

    this.RegisterHiddenField(“__VSTATE”, _newViewState);

    }

    }

    public class CompressToZip
    {

    public static byte[] Compress(byte[] _bytes)
    {
    MemoryStream _memStream = new MemoryStream();
    Zip.Streams.DeflaterOutputStream _stream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(_memStream,
    new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(Zip.Deflater.BEST_COMPRESSION), 131072);

    _stream.Write(_bytes, 0, _bytes.Length);
    _stream.Close();

    return _memStream.ToArray();
    }

    public static byte[] Decompress(byte[] _bytes)
    {

    Zip.Streams.InflaterInputStream _stream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(
    new MemoryStream(_bytes));

    MemoryStream _memStream = new MemoryStream();

    byte[] _writeData = new byte[4096];
    int _size = 0;

    while (true)
    {
    _size = _stream.Read(_writeData, 0, _writeData.Length);

    if (_size > 0)
    _memStream.Write(_writeData, 0, _size);
    else
    break;
    }

    _stream.Close();

    return _memStream.ToArray();
    }

    }

  3. The code was quite helpful…to improve the performance of the page ,which is quite overloaded….Thanks a lot for the code

  4. Wow! i agree! i’ve been searching for so long for a site where i could find everything that i want, and i’ve just found it!! really, i’ve visited your blog, and it’s amazing, i will keep visiting

Leave a Reply

Your email address will not be published. Required fields are marked *