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

前端 未结 3 1693
失恋的感觉
失恋的感觉 2020-12-21 01:13

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

相关标签:
3条回答
  • 2020-12-21 01:40

    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

    0 讨论(0)
  • 2020-12-21 01:40

    I think, after looking at the Reflector'd mechanism for Page and how it handles viewstate, that you're going to have to go to an HttpModule to get what you're after, if you want the actual viewstate size on the page.

    I say this because you're going to have to get the literal string from the page after it's been rendered, which doesn't happen till after all the user-definable events have triggered. See reflector output below (partial):

                this.PerformPreRenderComplete();
                if (context.TraceIsEnabled)
                {
                    this.Trace.Write("aspx.page", "End PreRenderComplete");
                }
                if (context.TraceIsEnabled)
                {
                    this.BuildPageProfileTree(this.EnableViewState);
                    this.Trace.Write("aspx.page", "Begin SaveState");
                }
                if (EtwTrace.IsTraceEnabled(5, 4))
                {
                    EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_SAVE_VIEWSTATE_ENTER, this._context.WorkerRequest);
                }
                this.SaveAllState();
                if (EtwTrace.IsTraceEnabled(5, 4))
                {
                    EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_SAVE_VIEWSTATE_LEAVE, this._context.WorkerRequest);
                }
                if (context.TraceIsEnabled)
                {
                    this.Trace.Write("aspx.page", "End SaveState");
                    this.Trace.Write("aspx.page", "Begin SaveStateComplete");
                }
                this.OnSaveStateComplete(EventArgs.Empty);
                if (context.TraceIsEnabled)
                {
                    this.Trace.Write("aspx.page", "End SaveStateComplete");
                    this.Trace.Write("aspx.page", "Begin Render");
                }
                if (EtwTrace.IsTraceEnabled(5, 4))
                {
                    EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RENDER_ENTER, this._context.WorkerRequest);
                }
                if (str != null)
                {
                    this.ExportWebPart(str);
                }
                else
                {
                    this.RenderControl(this.CreateHtmlTextWriter(this.Response.Output));
                }
                if (EtwTrace.IsTraceEnabled(5, 4))
                {
                    EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RENDER_LEAVE, this._context.WorkerRequest);
                }
                if (context.TraceIsEnabled)
                {
                    this.Trace.Write("aspx.page", "End Render");
                }
                this.CheckRemainingAsyncTasks(false);
    

    Otherwise, you can grab the viewstatebag and iterate over it's contents. That works well too, depending on how much detail you want to go into.

    0 讨论(0)
  • 2020-12-21 01:42

    You could use SaveStateCompleted, which occurs right after all the state has been saved to the viewstate. To know the size of the viewstate, just make a character count after calling ToString on the viewstate.

    ViewState.ToString.Count() 
    
    0 讨论(0)
提交回复
热议问题