IStateFormatter 接口
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义要对对象图进行序列化和反序列化,类型应实现的方法。
public interface class IStateFormatter
public interface IStateFormatter
type IStateFormatter = interface
Public Interface IStateFormatter
- 派生
示例
下面的代码示例演示如何创建一个 PageStatePersister 对象,该对象将视图和控制状态保存到 Web 服务器上的流。 该 StreamPageStatePersister
类演示如何重写 Load 提取和保存页面状态信息的和 Save 方法。 这些方法使用 IStateFormatter 从类继承的 PageStatePersister 接口来序列化和反序列化视图状态。 此代码示例是为类提供的大型示例的 PageStatePersister 一部分。
namespace Samples.AspNet.CS
{
using System;
using System.IO;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
//
// The StreamPageStatePersister is an example view state
// persistence mechanism that persists view and control
// state on the Web server.
//
[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
public class StreamPageStatePersister : PageStatePersister
{
public StreamPageStatePersister(Page page)
: base(page)
{
}
//
// Load ViewState and ControlState.
//
public override void Load()
{
Stream stateStream = GetSecureStream();
// Read the state string, using the StateFormatter.
StreamReader reader = new StreamReader(stateStream);
IStateFormatter formatter = this.StateFormatter;
string fileContents = reader.ReadToEnd();
// Deserilize returns the Pair object that is serialized in
// the Save method.
Pair statePair = (Pair)formatter.Deserialize(fileContents);
ViewState = statePair.First;
ControlState = statePair.Second;
reader.Close();
stateStream.Close();
}
//
// Persist any ViewState and ControlState.
//
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;
}
}
}
Imports System.IO
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI
Namespace Samples.AspNet.VB
' The StreamPageStatePersister is an example view state
' persistence mechanism that persists view and control
' state on the Web server.
'
<AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class StreamPageStatePersister
Inherits PageStatePersister
Public Sub New(ByVal page As Page)
MyBase.New(page)
End Sub
'
' Load ViewState and ControlState.
'
Public Overrides Sub Load()
Dim stateStream As Stream
stateStream = GetSecureStream()
' Read the state string, using the StateFormatter.
Dim reader As New StreamReader(stateStream)
Dim serializedStatePair As String
serializedStatePair = reader.ReadToEnd
Dim statePair As Pair
Dim formatter As IStateFormatter
formatter = Me.StateFormatter
' Deserilize returns the Pair object that is serialized in
' the Save method.
statePair = CType(formatter.Deserialize(serializedStatePair), Pair)
ViewState = statePair.First
ControlState = statePair.Second
reader.Close()
stateStream.Close()
End Sub
'
' Persist any ViewState and ControlState.
'
Public Overrides Sub Save()
If Not (ViewState Is Nothing) OrElse Not (ControlState Is Nothing) Then
If Not (Page.Session Is Nothing) Then
Dim stateStream As Stream
stateStream = GetSecureStream()
' Write a state string, using the StateFormatter.
Dim writer As New StreamWriter(stateStream)
Dim formatter As IStateFormatter
formatter = Me.StateFormatter
Dim statePair As New Pair(ViewState, ControlState)
Dim serializedState As String
serializedState = formatter.Serialize(statePair)
writer.Write(serializedState)
writer.Close()
stateStream.Close()
Else
Throw New InvalidOperationException("Session needed for StreamPageStatePersister.")
End If
End If
End Sub
' Return a secure Stream for your environment.
Private Function GetSecureStream() As Stream
' You must provide the implementation to build
' a secure Stream for your environment.
Return Nothing
End Function
End Class
End Namespace
注解
该IStateFormatter接口定义类型可以实现的方法,以序列化和反序列化 ASP.NET Web 服务器控件在其属性中ViewState维护的状态。 派生自类的PageStatePersister类使用此基础结构来维护请求之间的 ASP.NET 页面状态。 默认情况下,ASP.NET 页面状态由类的ObjectStateFormatter实例序列化和反序列化;但是,站点和适配器开发人员可以在自己的类型上实现IStateFormatter接口来执行这项工作。
有关 Web 服务器控件状态管理和视图状态的详细信息,请参阅 ASP.NET 状态管理概述和动态 Web 服务器控件和视图状态。
方法
Deserialize(String) |
从对象状态图的序列化字符串形式对其进行反序列化。 |
Serialize(Object) |
将 ASP.NET Web 服务器控件状态序列化为字符串形式。 |