Bagikan melalui


IStateFormatter Antarmuka

Definisi

Menentukan metode yang diterapkan jenis untuk menserialisasikan dan mendeserialisasi grafik objek.

public interface class IStateFormatter
public interface IStateFormatter
type IStateFormatter = interface
Public Interface IStateFormatter
Turunan

Contoh

Contoh kode berikut menunjukkan cara membuat PageStatePersister objek yang menyimpan tampilan dan status kontrol ke aliran di server Web. Kelas menunjukkan StreamPageStatePersister cara mengambil Load alih metode dan Save untuk mengekstrak dan menyimpan informasi status halaman. Metode ini menggunakan antarmuka yang IStateFormatter diwarisi dari PageStatePersister kelas untuk menserialisasikan dan mendeserialisasi status tampilan. Contoh kode ini adalah bagian dari contoh yang lebih besar yang disediakan untuk PageStatePersister kelas .

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

Keterangan

Antarmuka IStateFormatter mendefinisikan metode yang dapat diterapkan jenis untuk membuat serialisasi dan deserialisasi status yang dipertahankan kontrol server web ASP.NET dalam propertinya ViewState . Infrastruktur ini digunakan oleh kelas yang berasal dari PageStatePersister kelas untuk mempertahankan status halaman ASP.NET antar permintaan. Secara default, ASP.NET status halaman diserialisasikan dan dideserialisasi oleh instans ObjectStateFormatter kelas; namun, pengembang situs dan adaptor dapat mengimplementasikan IStateFormatter antarmuka pada jenis mereka sendiri untuk melakukan pekerjaan ini.

Untuk informasi selengkapnya tentang manajemen status kontrol server Web dan status tampilan, lihat ASP.NET Gambaran Umum Manajemen Status dan Kontrol Server Web Dinamis dan Status Tampilan.

Metode

Deserialize(String)

Mendeserialisasi grafik status objek dari bentuk string yang diserialisasikan.

Serialize(Object)

Menserialisasikan status kontrol server web ASP.NET ke formulir string.

Berlaku untuk

Lihat juga