0

读了一本书,我设法创建了一个类,该类用作基类来覆盖 Load 和 Save 方法,以提取视图状态信息并将其保存到服务器。然而,在本书中,作者建议转到以下链接并查看示例以执行以下操作:

您可以使用页面适配器,而不是使用页面基类。这将修改所有页面的功能,而无需更改它们的基类。如果您想在服务器上为所有页面存储 ViewState,那就太好了。要了解如何使用 PageAdapter 类的 GetStatePersister 方法为所有页面存储 ViewState,请访问:

我要做的就是找到一种方法来保存 ViewState 而无需手动更改每个页面上的基类。

我收到以下错误:

[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

这是执行此操作所引用的链接:

http://msdn.microsoft.com/en-us/library/system.web.ui.pagestatepersister.aspx

StreamPageStatePersister.cs 代码

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;
        }
    }
}

MyPageAdapter.cs 代码

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);
         }
} }

最后在浏览器文件中:

<browsers>
    <browser id="NewBrowser" parentID="Mozilla">
        <identification>
            <userAgent match="Unique User Agent Regular Expression" />
        </identification>

        <capture>
            <userAgent match="NewBrowser (?'version'\d+\.\d+)" />
        </capture>

        <capabilities>
            <capability name="browser" value="My New Browser" />
            <capability name="version" value="${version}" />
        </capabilities>
    </browser>

    <browser refID="Mozilla">
        <capabilities>
            <capability name="xml" value="true" />
        </capabilities>

      <controlAdapters>
        <adapter
            controlType="System.Web.UI.Page"
            adapterType="dev.MyPageAdapter" />
      </controlAdapters>
    </browser>  
</browsers>
4

1 回答 1

0

我认为您应该确定其余浏览器的适配器。你可以试试:

adapterType="Samples.AspNet.CS.MyPageAdapter" />

于 2014-01-29T23:18:06.037 回答