问题
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.
回答1:
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
回答2:
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()
回答3:
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.
来源:https://stackoverflow.com/questions/4046805/determine-size-of-asp-net-pages-viewstate-before-serving-page