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:
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 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.
'//--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()
Shared Function Decompress(ByVal bytes() As Byte) As Byte()
Dim stream = New Zip.Streams.InflaterInputStream(New MemoryStream(bytes))
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
Ads by The Lounge