title: How to create a PageStatePersister object that saves view and control state on the Web server
body: <p>Reading a book I managed to create a class which is than used as a base class to override the Load and Save methods to extract and save view state information to the server. However in the book the author suggests going to the following link and viewing the example to do the following:</p>
  
  <p><em>Instead of using a page base class, you could use a page adapter. This would modify
  the functionality of all pages without having to change their base classes. That's great
  if you want to store ViewState on the server for all pages. To find
  out how to store ViewState for all pages using the GetStatePersister method of the
  PageAdapter class, visit:</em></p>
  
  <p>All I am trying to do is find a way to save the ViewState without manually changing the base Class on each page.</p>
  
  <p>I get the following error:</p>
  
  <pre><code>[ArgumentNullException: Value cannot be null.
  Parameter name: stream]
     System.IO.StreamWriter..ctor(Stream stream, Encoding encoding, Int32 bufferSize, Boolean leaveOpen) +10409245
     System.IO.StreamWriter..ctor(Stream stream) +30
     dev.StreamPageStatePersister.Save() in a:\Project\Application\Web\dev\StreamPageStatePersister.cs:57
     System.Web.UI.Page.SavePageStateToPersistenceMedium(Object state) +108
     System.Web.UI.Page.SaveAllState() +659
     System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1225
  </code></pre>
  
  <p>Here is the link referenced to do this:</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/system.web.ui.pagestatepersister.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.web.ui.pagestatepersister.aspx</a></p>
  
  <p><em>StreamPageStatePersister.cs Code</em></p>
  
  <pre><code>namespace dev
  {
  
      using System;
      using System.IO;
      using System.Security.Permissions;
      using System.Web;
      using System.Web.UI;
  
      [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
      public class StreamPageStatePersister : PageStatePersister
      {
  
          public StreamPageStatePersister(Page page)
              : base(page)
          {
          }
  
          public override void Load()
          {
              Stream stateStream = GetSecureStream();
  
              StreamReader reader = new StreamReader(stateStream);
  
              IStateFormatter formatter = this.StateFormatter;
              string fileContents = reader.ReadToEnd();
  
              Pair statePair = (Pair)formatter.Deserialize(fileContents);
  
              ViewState = statePair.First;
              ControlState = statePair.Second;
              reader.Close();
              stateStream.Close();
          }
  
          public override void Save()
          {
  
              if (ViewState != null || ControlState != null)
              {
                  if (Page.Session != null)
                  {
                      Stream stateStream = GetSecureStream();
  
                      StreamWriter writer = new StreamWriter(stateStream);
  
                      IStateFormatter formatter = this.StateFormatter;
                      Pair statePair = new Pair(ViewState, ControlState);
  
                      // Serialize the statePair object to a string. 
                      string serializedState = formatter.Serialize(statePair);
  
                      writer.Write(serializedState);
                      writer.Close();
                      stateStream.Close();
                  }
                  else
                      throw new InvalidOperationException("Session needed for StreamPageStatePersister.");
              }
          }
          // Return a secure Stream for your environment. 
          private Stream GetSecureStream()
          {
              // You must provide the implementation to build 
              // a secure Stream for your environment. 
              return null;
          }
      }
  }
  </code></pre>
  
  <p><em>MyPageAdapter.cs Code</em></p>
  
  <pre><code>namespace dev {
  
       using System.Security.Permissions;
       using System.Web;
       using System.Web.UI;
  
       [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
      public class MyPageAdapter : System.Web.UI.Adapters.PageAdapter
      {
  
          public override PageStatePersister GetStatePersister()
          {
              return new dev.StreamPageStatePersister(Page);
           }
  } }
  </code></pre>
  
  <p>Finally in the browser file:</p>
  
  <pre><code>&lt;browsers&gt;
      &lt;browser id="NewBrowser" parentID="Mozilla"&gt;
          &lt;identification&gt;
              &lt;userAgent match="Unique User Agent Regular Expression" /&gt;
          &lt;/identification&gt;
  
          &lt;capture&gt;
              &lt;userAgent match="NewBrowser (?'version'\d+\.\d+)" /&gt;
          &lt;/capture&gt;
  
          &lt;capabilities&gt;
              &lt;capability name="browser" value="My New Browser" /&gt;
              &lt;capability name="version" value="${version}" /&gt;
          &lt;/capabilities&gt;
      &lt;/browser&gt;
  
      &lt;browser refID="Mozilla"&gt;
          &lt;capabilities&gt;
              &lt;capability name="xml" value="true" /&gt;
          &lt;/capabilities&gt;
  
        &lt;controlAdapters&gt;
          &lt;adapter
              controlType="System.Web.UI.Page"
              adapterType="dev.MyPageAdapter" /&gt;
        &lt;/controlAdapters&gt;
      &lt;/browser&gt;  
  &lt;/browsers&gt;
  </code></pre>