Click here to Skip to main content
11,413,548 members (74,070 online)
Click here to Skip to main content

ViewState Compression

, 10 Jul 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
How to compress the ViewState of ASP.NET pages and save bandwidth.

Introduction

Recently, I developed a huge ASP.NET page, with more than 30 controls. As we all know, it's a good idea to disable the ViewState for the controls that don't actually need it, say Literals or Labels. After doing that, I noticed that the hidden ViewState field was still a few KBs big. This is obviously a big problem for the users that still don't have a broadband connection, because uploading 40 KB to the server is really a bad issue, especially when they begin to click the "Submit" button again and again because they don't notice any response. So, after a few searches through the Internet, I built a simple solution to compress the ViewState and therefore save a rough 50% of the bandwidth. This post by Scott Hanselman has been particularly useful. Although it's possible to use external libraries to perform compression tasks, I think the better solution is to use the GZipStream or DeflateStream that the .NET Framework 2.0 includes.

Compressing and Decompressing Data in Memory

First of all, we need a way to compress and decompress an array of bytes in memory. I put together this simple static class that exposes two methods: Compress and Decompress. The two available classes, GZipStream and DeflateStream, according to MSDN, use the same algorithm, so it's irrelevant which one you choose.

The code below is really simple, and doesn't need further explanation:

using System.IO;
using System.IO.Compression;

public static class Compressor {

  public static byte[] Compress(byte[] data) {
    MemoryStream output = new MemoryStream();
    GZipStream gzip = new GZipStream(output, 
                      CompressionMode.Compress, true);
    gzip.Write(data, 0, data.Length);
    gzip.Close();
    return output.ToArray();
  }

  public static byte[] Decompress(byte[] data) {
    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();
  }
}

You need to save this class in a .cs file and put it in the App_Code directory of your ASP.NET application, making sure it's contained in the proper custom namespace (if you don't specify any namespace, the class will be available in the built-in ASP namespace).

Compressing the ViewState

Now, we can actually compress the ViewState of the page. To do that, we have to override the two methods LoadPageStateFromPersistenceMedium and SavePageStateToPersistenceMedium. The code simply uses an additional hidden field, __VSTATE, to store the compressed ViewState. As you can see, by viewing the HTML of the page, the __VIEWSTATE field is empty, while our __VSTATE field contains the compressed ViewState, encoded in Base64. Let's see the code.

public partial class MyPage : System.Web.UI.Page {

  protected override object LoadPageStateFromPersistenceMedium() {
    string viewState = Request.Form["__VSTATE"];
    byte[] bytes = Convert.FromBase64String(viewState);
    bytes = Compressor.Decompress(bytes);
    LosFormatter formatter = new LosFormatter();
    return formatter.Deserialize(Convert.ToBase64String(bytes));
  }

  protected override void SavePageStateToPersistenceMedium(object viewState) {
    LosFormatter formatter = new LosFormatter();
    StringWriter writer = new StringWriter();
    formatter.Serialize(writer, viewState);
    string viewStateString = writer.ToString();
    byte[] bytes = Convert.FromBase64String(viewStateString);
    bytes = Compressor.Compress(bytes);
    ClientScript.RegisterHiddenField("__VSTATE", Convert.ToBase64String(bytes));
  }

  // The rest of your code here...
}

In the first method, we just decode from Base64, decompress and deserialize the content of the __VSTATE, and return it to the runtime. In the second method, we perform the opposite operation: serialize, compress, and encode in Base64. The Base64 string is then saved into the __VSTATE hidden field. The LosFormatter object performs the serialization and deserialization tasks.

You may also want to create a new class, for example, CompressedPage, inheriting from System.Web.UI.Page, in which you override the two methods and then inherit your page from that class, for example MyPage : CompressedPage. Just remember that .NET has only single inheritance, and by following this way, you "spend" your only inheritance chance to use the ViewState compression. On the other hand, overriding the two methods in every class is a waste of time, so you have to choose the way that best fits your needs.

Performances and Conclusions

After a few tests, I noticed that the ViewState has been reduced from 38 KB to 17 KB, saving 44%. Supposing you have an average of 1 postback per minute per user, you could save more than 885 MB of bandwidth per month on every single user. That's an excellent result: you save bandwidth (and therefore money), and the user notices a shorter server response time.

I wanted to point out that this solution has a performance hit on the server's hardware. Compressing, decompressing, encoding, and decoding data is quite a heavy work for the server, so you have to balance the number of users with your CPU power and RAM.

License

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

Share

About the Author

Dario Solera

Italy Italy
Software Development Manager working on IaaS cloud computing. Cloud believer, (former) entrepreneur, F1 addict.

Follow me at dariosolera.it or on Twitter.
Follow on   Twitter   Google+

Comments and Discussions

 
Generalgood stuff Pin
mrkyle at 28-Feb-08 19:39
membermrkyle28-Feb-08 19:39 
GeneralExcellent Article..!! Pin
Sujith John Thomas at 21-Jul-07 3:18
memberSujith John Thomas21-Jul-07 3:18 
GeneralAnother way [modified] Pin
boros24 at 22-Feb-07 4:13
memberboros2422-Feb-07 4:13 
GeneralRe: Another way Pin
Munsifali Rashid at 28-Oct-07 12:02
memberMunsifali Rashid28-Oct-07 12:02 
GeneralRe: Another way Pin
Fahad Azeem at 25-Apr-08 6:47
memberFahad Azeem25-Apr-08 6:47 
GeneralASP.NET AJAX 1.0 Problem Pin
lookaround at 15-Feb-07 3:48
memberlookaround15-Feb-07 3:48 
GeneralRe: ASP.NET AJAX 1.0 Problem Pin
DAJG at 10-Jul-07 9:06
memberDAJG10-Jul-07 9:06 
GeneralRe: ASP.NET AJAX 1.0 Problem Pin
uyildir at 11-Nov-07 11:08
memberuyildir11-Nov-07 11:08 
GeneralRe: ASP.NET AJAX 1.0 Problem Pin
CoolVini at 7-Oct-08 0:16
memberCoolVini7-Oct-08 0:16 
GeneralRe: ASP.NET AJAX 1.0 Problem Pin
kjerolran at 5-Mar-09 11:02
memberkjerolran5-Mar-09 11:02 
GeneralRe: ASP.NET AJAX 1.0 Problem Pin
dhejov at 15-Aug-10 20:39
memberdhejov15-Aug-10 20:39 
GeneralVB.Net version using ICSharpCode.SharpZipLib Pin
aka2005 at 27-Nov-06 9:26
memberaka200527-Nov-06 9:26 
Questiondose it make server busy? Pin
huobazi@aspxboy.com at 25-Nov-06 7:02
memberhuobazi@aspxboy.com25-Nov-06 7:02 
AnswerRe: dose it make server busy? Pin
adriancs at 3-Jun-14 21:14
professionaladriancs3-Jun-14 21:14 
GeneralExcellent. Pin
deepaks3 at 28-Aug-06 22:13
memberdeepaks328-Aug-06 22:13 
GeneralServer Side Pin
jonavi at 18-Jul-06 3:08
memberjonavi18-Jul-06 3:08 
GeneralRe: Server Side Pin
Dario Solera at 18-Jul-06 3:24
memberDario Solera18-Jul-06 3:24 
GeneralRe: Server Side Pin
jonavi at 18-Jul-06 3:29
memberjonavi18-Jul-06 3:29 
GeneralRe: Server Side Pin
Dario Solera at 18-Jul-06 3:36
memberDario Solera18-Jul-06 3:36 
GeneralRe: Server Side Pin
jonavi at 18-Jul-06 3:38
memberjonavi18-Jul-06 3:38 
GeneralRe: Server Side Pin
Dario Solera at 18-Jul-06 3:47
memberDario Solera18-Jul-06 3:47 
GeneralRe: Server Side Pin
jonavi at 18-Jul-06 4:38
memberjonavi18-Jul-06 4:38 
GeneralRe: Server Side Pin
Dario Solera at 18-Jul-06 22:46
memberDario Solera18-Jul-06 22:46 
GeneralRe: Server Side Pin
jonavi at 18-Jul-06 4:16
memberjonavi18-Jul-06 4:16 
GeneralRe: Server Side Pin
Dario Solera at 18-Jul-06 22:45
memberDario Solera18-Jul-06 22:45 
GeneralRe: Server Side Pin
rutgervanhagen at 4-Jan-07 22:22
memberrutgervanhagen4-Jan-07 22:22 
GeneralRe: Server Side Pin
Mark Douglas at 18-Jul-06 23:34
memberMark Douglas18-Jul-06 23:34 
GeneralRe: Server Side Pin
Dario Solera at 18-Jul-06 23:36
memberDario Solera18-Jul-06 23:36 
GeneralRe: Server Side Pin
Mark Douglas at 18-Jul-06 23:41
memberMark Douglas18-Jul-06 23:41 
GeneralRe: Server Side Pin
Roger Willcocks at 19-Jul-06 13:29
memberRoger Willcocks19-Jul-06 13:29 
GeneralRe: Server Side Pin
Mark Douglas at 19-Jul-06 22:47
memberMark Douglas19-Jul-06 22:47 
GeneralRe: Server Side Pin
Roger Willcocks at 20-Jul-06 1:49
memberRoger Willcocks20-Jul-06 1:49 
GeneralRe: Server Side Pin
jonavi at 18-Jul-06 3:24
memberjonavi18-Jul-06 3:24 
GeneralRe: Server Side Pin
Dario Solera at 18-Jul-06 3:29
memberDario Solera18-Jul-06 3:29 
QuestionRe: Server Side Pin
jonavi at 18-Jul-06 7:00
memberjonavi18-Jul-06 7:00 
AnswerRe: Server Side Pin
Dario Solera at 18-Jul-06 22:50
memberDario Solera18-Jul-06 22:50 
jonavi wrote:
I think I have a mistake, maybe you can see it....


I don't see errors, sorry.

jonavi wrote:
Lastly, I was looking at your GZIp page load implementation, and I liked it, have you used http compression along with viewstate compression? does one mess with the other?


I use them together and they work really fine. Remember that HTTP compression works only from server to client, so if you don't compress the ViewState the postbacks still remain slow.

_____________________________________________
Tozzi is right: Gaia is getting rid of us.
My Blog [ITA] - Developing ScrewTurn Wiki 1.0 Beta3

GeneralViewState Pin
Simon 1979 at 17-Jul-06 7:36
memberSimon 197917-Jul-06 7:36 
GeneralExcellent Article Pin
Krishna Pendoti at 11-Jul-06 12:14
memberKrishna Pendoti11-Jul-06 12:14 
GeneralRe: Excellent Article Pin
Dario Solera at 11-Jul-06 12:20
memberDario Solera11-Jul-06 12:20 
GeneralVery nice Pin
WillemM at 11-Jul-06 5:54
memberWillemM11-Jul-06 5:54 
GeneralRe: Very nice Pin
Dario Solera at 11-Jul-06 5:58
memberDario Solera11-Jul-06 5:58 
GeneralArticle for ASP.Net 2.0 Pin
DeltaSoft at 10-Jul-06 21:05
memberDeltaSoft10-Jul-06 21:05 
GeneralRe: Article for ASP.Net 2.0 [modified] Pin
Dario Solera at 10-Jul-06 21:58
memberDario Solera10-Jul-06 21:58 
AnswerRe: Article for ASP.Net 2.0 Pin
Jasper K. at 18-Jul-06 23:22
memberJasper K.18-Jul-06 23:22 
GeneralConsider HTTP compression Pin
petecooper at 10-Jul-06 20:35
memberpetecooper10-Jul-06 20:35 
GeneralRe: Consider HTTP compression Pin
Dario Solera at 10-Jul-06 21:53
memberDario Solera10-Jul-06 21:53 
GeneralRe: Consider HTTP compression Pin
petecooper at 11-Jul-06 5:35
memberpetecooper11-Jul-06 5:35 
GeneralRe: Consider HTTP compression Pin
daniero at 11-Jul-06 7:30
memberdaniero11-Jul-06 7:30 
GeneralRe: Consider HTTP compression Pin
Dario Solera at 11-Jul-06 7:36
memberDario Solera11-Jul-06 7:36 
GeneralRe: Consider HTTP compression Pin
petecooper at 11-Jul-06 7:37
memberpetecooper11-Jul-06 7:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150427.1 | Last Updated 10 Jul 2006
Article Copyright 2006 by Dario Solera
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid