Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine size of ASP.NET page's viewstate before serving page

What ASP.NET page lifecycle event can I write code in to determine the size of the viewstate that being sent out? Also, is it possible to determine the size without parsing through the rendered HTML (like a property on the page object) or is parsing the only way?

What I'd like to do is log the sizes, specifically if they cross a certain threshold.

like image 659
slolife Avatar asked Oct 13 '22 21:10

slolife


1 Answers

You can go on the function that is going to writing the viewstate, the SavePageStateToPersistenceMedium. This is the function that also used to compress viewstate...

For example...

public abstract class BasePage : System.Web.UI.Page
{
    private ObjectStateFormatter _formatter = new ObjectStateFormatter();

    protected override void SavePageStateToPersistenceMedium(object viewState)
    {
        MemoryStream ms = new MemoryStream();    
        _formatter.Serialize(ms, viewState);    
        byte[] viewStateArray = ms.ToArray();

        ....

    }
}

Some reference.
http://www.codeproject.com/KB/viewstate/ViewStateCompression.aspx
http://forums.asp.net/p/1139883/3836512.aspx
http://www.dotnetcurry.com/ShowArticle.aspx?ID=67&AspxAutoDetectCookieSupport=1

like image 122
Aristos Avatar answered Nov 15 '22 08:11

Aristos