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