IStateFormatter 인터페이스

정의

개체 그래프를 직렬화 및 역직렬화하기 위해 형식에서 구현하는 메서드를 정의합니다.

public interface class IStateFormatter
public interface IStateFormatter
type IStateFormatter = interface
Public Interface IStateFormatter
파생

예제

다음 코드 예제에는 만드는 방법을 보여 줍니다는 PageStatePersister 스트림으로 웹 서버의 뷰 및 컨트롤 상태를 저장 하는 개체입니다. StreamPageStatePersister 클래스에 재정의 하는 방법을 보여 줍니다. 합니다 LoadSave 압축을 풀고 저장 방법 페이지 상태 정보입니다. 이러한 메서드를 사용 합니다 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 웹 서버 컨트롤에서 유지 관리 하는 상태를 역직렬화하기 위해 구현할 수 있는 메서드를 정의 하는 인터페이스의 ViewState 속성입니다. 이 인프라에서 파생 된 클래스에서 사용 되는 PageStatePersister 요청 사이는 ASP.NET 페이지의 상태를 유지 하는 클래스입니다. 기본적으로 ASP.NET 페이지 상태는 직렬화 / 역직렬화 할 인스턴스의 합니다 ObjectStateFormatter 클래스, 사이트 및 어댑터 개발자가 구현할 수 있지만 IStateFormatter 이 작업을 수행 하는 자체 형식에 대 한 인터페이스입니다.

웹 서버 컨트롤 상태 관리 및 보기의 상태에 대 한 자세한 내용은 참조 하세요. ASP.NET 상태 관리 개요 하 고 동적 웹 서버 컨트롤 및 View State합니다.

메서드

Deserialize(String)

직렬화된 문자열 형식에서 개체 상태 그래프를 역직렬화합니다.

Serialize(Object)

ASP.NET 웹 서버 컨트롤 상태를 문자열 형식으로 serialize합니다.

적용 대상

추가 정보