Asp.net processes excessive viewstate code in the original file.

Source: Internet
Author: User

Copy codeThe Code is as follows:
Public class XVPage: Page
{
Static private DirectoryInfo _ Dir;
Private DirectoryInfo Dir
{
Get
{
If (_ Dir = null)
{
_ Dir = new DirectoryInfo (Path. Combine (AppDomain. CurrentDomain. BaseDirectory, "App_Data "));
If (! _ Dir. Exists)
_ Dir. Create ();
_ Dir = new DirectoryInfo (Path. Combine (_ Dir. FullName, "ViewState "));
If (! _ Dir. Exists)
_ Dir. Create ();
}
Return _ Dir;
}
}
Protected override object LoadPageStateFromPersistenceMedium ()
{
PageStatePersister ps = this. PageStatePersister;
Ps. Load ();
If (ps. ControlState! = Null)
Ps. ControlState = AntiSerialization (string) ps. ControlState );
If (ps. ViewState! = Null)
Ps. ViewState = AntiSerialization (string) ps. ViewState );
Return new Pair (ps. ControlState, ps. ViewState );
}
Protected override void SavePageStateToPersistenceMedium (object state)
{
PageStatePersister ps = this. PageStatePersister;
If (state is Pair)
{
Pair pair = (Pair) state;
Ps. ControlState = pair. First;
Ps. ViewState = pair. Second;
}
Else
{
Ps. ViewState = state;
}
If (ps. ControlState! = Null)
Ps. ControlState = AntiSerialization (ps. ControlState );
If (ps. ViewState! = Null)
Ps. ViewState = AntiSerialization (ps. ViewState );
Ps. Save ();
}
Private object AntiSerialization (string stateID)
{
String stateStr = (string) Cache [stateID];
String file = Path. Combine (Dir. FullName, stateID );
If (stateStr = null)
StateStr = File. ReadAllText (file );
Else
Cache. Remove (stateID );
Return new ObjectStateFormatter (). Deserialize (stateStr );
}
Private string AntiSerialization (object obj)
{
String value = new ObjectStateFormatter (). Serialize (obj );
String stateID = (DateTime. Now. Ticks + (long) value. GetHashCode (). ToString (); // generates a discrete id number.
File. WriteAllText (Path. Combine (Dir. FullName, stateID), value );
Cache. Insert (stateID, value );
Return stateID;
}
Protected override void OnUnload (EventArgs e)
{
Base. OnUnload (e );
DateTime dt = DateTime. Now. AddMinutes (-20 );
Foreach (FileInfo fl in Dir. GetFiles ())
If (fl. LastAccessTime <dt)
Try
{
Fl. Delete ();
}
Catch
{
}
}
}


You only need to inherit XVPage in the background of the page.
Public partial class Index_Content: XVPage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.