Monday, August 22, 2005

A while back, I showed you my file-based viewstate persistence solution.  Thanks to Google search hits and traffic from Scott Hanselman's analysis, it's been one of my more popular entries.  With ASP.net's improvements in this area, I felt that it was due for an update.  So, here's a quick whack at it.  The usual disclaimers apply.

2.0 adds the notion of "control state" to state persistence, which is very cool.  It's an opt-in mechanism for things that need to survive postbacks even if ViewState is turned off.  In addition, there's some new flexibility with page adapters and such, but we'll ignore that complexity for now and go for a direct port of my old sample, but include the new ControlState mechanism.

public class FileBasedPageStatePersister : HiddenFieldPageStatePersister {

      public FileBasedPageStatePersister(Page page) : base(page) {}

 

      public override void Load() {

            //let the base class do its thing

            base.Load();

            //get the control state

            object baseControlState = base.ControlState;

            if (baseControlState != null) {

                  //the control state should be our Guid

                  Guid guid = (Guid)baseControlState;

                  //read the contents of the file and set the two states

                  using (TextReader reader = new StreamReader(CreateOfflineViewStateFilePath(guid))) {

                        Pair pair = this.StateFormatter.Deserialize(reader.ReadToEnd()) as Pair;

                        base.ViewState = pair.First;

                        base.ControlState = pair.Second;

                  }

            }

      }

 

      public override void Save() {

            //create a guid for this viewstate

            Guid guid = Guid.NewGuid();

 

            //serialize the states into a temp file

            using (TextWriter writer = new StreamWriter(CreateOfflineViewStateFilePath(guid))) {

                  Pair pair = new Pair(base.ViewState, base.ControlState);

                  writer.Write(this.StateFormatter.Serialize(pair));

            }

            //trick the normal system into thinking all it needs to save is the guid

            base.ControlState = guid;

            base.ViewState = null;

            base.Save();

      }

 

      string CreateOfflineViewStateFilePath(Guid guid) {

            //TODO: put these files whereever you like

            return Path.Combine(Path.GetTempPath(), string.Format("{0}.viewstate", guid));

      }

 

}

So, we immediately see that it's much shorter.  This is because ASP.net uses a mechanism very similar to my 1.1 solution, so alot of the plumbing is built-in. The only thing you need to do to use it is override the PageStatePersister property on Page and return one of these. Again, we're piggybacking on the "hidden field" persistence mechanism and using that to store our Guid for the request.  Not much different, and I'm pretty happy to say that converting to this model from my old is very simple.

Another interesting idea would be to leave the ControlState in the hidden field, and only store the ViewState in the file. it would be a simple change that I'll leave as an exercise to you.  Then, you could be very aggresive about purging old or large files without worrying about breaking anything (provided of course that you've made your controls tolerant to such a method).

posted on Monday, August 22, 2005 1:21:14 PM (Pacific Standard Time, UTC-08:00)  #    Comments [0]
Related posts:
Happy Birthday Landon!
Happy Birthday, Jenna!
Random fun book thing and CLR In-Proc SxS
9-year Anniversary (yesterday)
LinqToStdf now on CodePlex
Image Slicer for Deep Zoom in Silverlight 2
Tracked by:
"used designing women cookbook" (used designing women cookbook) [Trackback]
"home sales in orlando" (home sales in orlando) [Trackback]
"hauling big game out" (hauling big game out) [Trackback]
"jewish webcasting" (jewish webcasting) [Trackback]
"king size bedding" (king size bedding) [Trackback]
"orgasm machine" (orgasm machine) [Trackback]
"VITAMIN C" (VITAMIN C) [Trackback]
"olympic limosine" (olympic limosine) [Trackback]
"definicion de xenical" (definicion de xenical) [Trackback]
"Stuttgart%2C Germany" (Stuttgart%2C Germany) [Trackback]
"flood safety" (flood safety) [Trackback]
"mazda miata for sale" (mazda miata for sale) [Trackback]
"Free Ringtones for My Sanyo 8100 Phone" (Free Ringtones for My Sanyo 8100 Phone... [Trackback]
"xm radio" (xm radio) [Trackback]
"ganson handbag" (ganson handbag) [Trackback]
"professional practice valuation" (professional practice valuation) [Trackback]
"MBA school rankings" (MBA school rankings) [Trackback]
"michigan state football" (michigan state football) [Trackback]
"accutane verdict" (accutane verdict) [Trackback]
"san diego tourism" (san diego tourism) [Trackback]
"foto alba parietti" (foto alba parietti) [Trackback]
"Wrestling Ladies Submission Holds" (Wrestling Ladies Submission Holds) [Trackback]
"bacterial meningitis settlement" (bacterial meningitis settlement) [Trackback]
"wn1151 tag heuer" (wn1151 tag heuer) [Trackback]
"electronic timesheet" (electronic timesheet) [Trackback]
"adult vacation submissive escort" (adult vacation submissive escort) [Trackback]
"ginko biloba for ed" (ginko biloba for ed) [Trackback]
"freckled lesbians" (freckled lesbians) [Trackback]
"guideline HIV AIDS Treatments" (guideline HIV AIDS Treatments) [Trackback]
Name
E-mail
(will show your gravatar icon)
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):