Scott Hanselman

Zipping/Compressing ViewState in ASP.NET

March 30, 2005 Comment on this post [7] Posted in ASP.NET | DasBlog | ViewState | HttpModule | Bugs
Sponsored By

Here's an interesting, odd, but obvious idea. If you're not able to use HttpCompression like the Blowery HttpCompression module that we use with dasBlog due to the pile of bugs in older versions of IE around compression, you can "zip" up the ViewState on fat (usually DataGrid related bloat).

This is some VB code that Vlad Olifier did, that he said I could post on my blog. It's also a new submission at GotDotnet. To use it, you just derive your ASP.NET page class from it.

Using Zipped ViewState:

Public Class CompressPage
  Inherits PageViewStateZip

Just deriving from System.Web.UI.Page as usual:

Public Class RegularPage
  Inherits System.Web.UI.Page

The "trick" is pretty simple. There are little-known virtuals in Page that you can override - specifically LoadPageStateFromPersistanceMedium (where that persistance medium is a hidden input box) and SavePageStateToPersistenceMedium.

When it's time to load view state, we pull it out of the form and un-base64 the string into a byte array, un-zip the bytes, then deserialize the ViewState. When it's time to save, reverse the process - Serialize, zip, store. There's some overhead, certainly. The amount of compression is usually about 50%, but your mileage may vary (YMMV).

The real decision flow is this:

  • Can you use HttpCompression (via XCompress or an HttpModule)?
    • If so, use it.
  • Can't? (Bugs, SSL, Compatibility, bosses, don't want to take the perf hit, etc)
    • Got Fat ViewState on a few pages?
      • Use Zipped ViewState on a few pages as needed.
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

Note that this sample uses SharpZipLib from ICSharpCode, but I assume you could use others, and I'd probably use System.IO.Compression if I was using .NET 2.0. The buffer sizes are hard-coded, but the only one that really matters it the one in Compress(). Again, salt to taste.

Imports System.IO
Imports Zip = ICSharpCode.SharpZipLib.Zip.Compression
'//--Download ICSharpCode.SharpZipLib from
'//--http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
 
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

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
March 30, 2005 18:56
Nice... I use a similar technique in my PHP framework (which is actually ASP.NET based).

Store state:
base64_encode(gzdeflate(serialize($this->_state)))

Read state:
unserialize(gzinflate(base64_decode($postedState)))
March 31, 2005 23:21
I use something similar but in a module to compress viewstate only on the fly (it's actually easier to just store it to a sql server and not send the viewstate at all, but that requires a server)

See http://philiprieck.com/blog/archive/2004/06/17/ViewRestate.aspx
April 01, 2005 0:46
You can also keep viewstate on the server. Robert Boedingheimer did some work with this, and I also ran some interesting tests on different alternatives.

http://www.eggheadcafe.com/articles/20040613.asp
September 06, 2005 18:52
Storing ViewState on Server is not a bad idea but what if in case of a Webfarm Scenario ?
Compressed transmission is the only alternative in that case.
-Mohan
November 23, 2005 13:28
I like this article, so much that I port the code into .net 2.0 and additional feature in it. See my blogspot (http://tuldoklambat.blogspot.com)
December 22, 2005 21:28
This is a nice solution. However, it can cause problems with request validation. Sometimes 'A potentially dangerous Request.Form' value is detected since the compressed viewstate can contain slashes. I attempted to use UrlEncode to fix this, but it causes invalid viewstate errors on loading.
January 18, 2006 0:20
C# 2.0 version I cobbled together

using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Compression;
using System.IO;

namespace YourNamespace.Web.Compression
{
public class ViewStateCompressor
{
static public byte[] Compress(byte[] buffer)
{
MemoryStream ms = new MemoryStream();

// Use the newly created memory stream for the compressed data.
GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress);

compressedzipStream.Write(buffer, 0, buffer.Length);

compressedzipStream.Close();

return ms.ToArray();
}

static public byte[] Inflate(byte[] buffer)
{
MemoryStream ms = new MemoryStream();
ms.Write(buffer, 0, buffer.Length);
ms.Seek(0, SeekOrigin.Begin);

GZipStream zipStream = new GZipStream(ms, CompressionMode.Decompress);

List&ltbyte> inflated = new List&ltbyte>();

while (true)
{
int b = zipStream.ReadByte();

if (b == -1)
break;

inflated.Add((byte)b);
}

return inflated.ToArray();
}
}
}


protected override object LoadPageStateFromPersistenceMedium()
{
string vState = Request.Form["__VSTATE"];
byte[] bytes = System.Convert.FromBase64String(vState);
bytes = ViewStateCompressor.Inflate(bytes);

LosFormatter format = new LosFormatter();
return format.Deserialize(System.Convert.ToBase64String(bytes));
}

protected override void SavePageStateToPersistenceMedium(object state)
{
LosFormatter format = new LosFormatter();
StringWriter writer = new StringWriter();

format.Serialize(writer, state);
string viewStateStr = writer.ToString();

byte[] bytes = System.Convert.FromBase64String(viewStateStr);

bytes = ViewStateCompressor.Compress(bytes);
string vStateStr = System.Convert.ToBase64String(bytes);
ClientScript.RegisterHiddenField("__VSTATE", vStateStr);
}

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.