如何实现
其实比较简单,在ASP.NET 2.0 里, 抽象类PageStatePersister 实现了存储page state到其它地方的基本功能,你只要重写这个抽象类提供的Load和Save方法就可以了。
我们看看DNN开源项目实现的一个CachePageStatePersister:
Imports System.Text
Namespace DotNetNuke.Framework
''' -----------------------------------------------------------------------------
''' Namespace: DotNetNuke.Framework
''' Project: DotNetNuke
''' Class: CachePageStatePersister
''' -----------------------------------------------------------------------------
''' <summary>
''' CachePageStatePersister provides a cache based page state peristence mechanism
''' </summary>
''' <history>
''' [cnurse] 11/30/2006 documented
''' </history>
''' -----------------------------------------------------------------------------
Public Class CachePageStatePersister
Inherits PageStatePersister
Private Const VIEW_STATE_CACHEKEY As String = "__VIEWSTATE_CACHEKEY"
''' -----------------------------------------------------------------------------
''' <summary>
''' Creates the CachePageStatePersister
''' </summary>
''' <history>
''' [cnurse] 11/30/2006 Documented
''' </history>
''' -----------------------------------------------------------------------------
Public Sub New(ByVal page As Page)
MyBase.New(page)
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' Loads the Page State from the Cache
''' </summary>
''' <history>
''' [cnurse] 11/30/2006 Documented
''' </history>
''' -----------------------------------------------------------------------------
Public Overrides Sub Load()
' Get the cache key from the web form data
Dim key As String = TryCast(Page.Request.Params(VIEW_STATE_CACHEKEY), String)
'Abort if cache key is not available or valid
If String.IsNullOrEmpty(key) Or Not key.StartsWith("VS_") Then
Throw New ApplicationException("Missing valid " + VIEW_STATE_CACHEKEY)
End If
Dim state As Pair = TryCast(DataCache.GetPersistentCacheItem(key, GetType(Pair)), Pair)
If Not state Is Nothing Then
'Set view state and control state
ViewState = state.First
ControlState = state.Second
End If
'Remove this ViewState from the cache as it has served its purpose
DataCache.RemovePersistentCacheItem(key)
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' Saves the Page State to the Cache
''' </summary>
''' <history>
''' [cnurse] 11/30/2006 Documented
''' </history>
''' -----------------------------------------------------------------------------
Public Overrides Sub Save()
'No processing needed if no states available
If ViewState Is Nothing And ControlState Is Nothing Then
Exit Sub
End If
'Generate a unique cache key
Dim key As New StringBuilder()
With key
.Append("VS_")
.Append(IIf(Page.Session Is Nothing, Guid.NewGuid().ToString(), Page.Session.SessionID))
.Append("_")
.Append(DateTime.Now.Ticks.ToString())
End With
'Save view state and control state separately
Dim state As New Pair(ViewState, ControlState)
'Add view state and control state to cache
DataCache.SetCache(key.ToString(), state, Nothing, DateTime.Now.AddMinutes(Page.Session.Timeout), System.Web.Caching.Cache.NoSlidingExpiration, True)
'Register hidden field to store cache key in
Page.ClientScript.RegisterHiddenField(VIEW_STATE_CACHEKEY, key.ToString())
End Sub
End Class
End Namespace
Namespace DotNetNuke.Framework
''' -----------------------------------------------------------------------------
''' Namespace: DotNetNuke.Framework
''' Project: DotNetNuke
''' Class: CachePageStatePersister
''' -----------------------------------------------------------------------------
''' <summary>
''' CachePageStatePersister provides a cache based page state peristence mechanism
''' </summary>
''' <history>
''' [cnurse] 11/30/2006 documented
''' </history>
''' -----------------------------------------------------------------------------
Public Class CachePageStatePersister
Inherits PageStatePersister
Private Const VIEW_STATE_CACHEKEY As String = "__VIEWSTATE_CACHEKEY"
''' -----------------------------------------------------------------------------
''' <summary>
''' Creates the CachePageStatePersister
''' </summary>
''' <history>
''' [cnurse] 11/30/2006 Documented
''' </history>
''' -----------------------------------------------------------------------------
Public Sub New(ByVal page As Page)
MyBase.New(page)
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' Loads the Page State from the Cache
''' </summary>
''' <history>
''' [cnurse] 11/30/2006 Documented
''' </history>
''' -----------------------------------------------------------------------------
Public Overrides Sub Load()
' Get the cache key from the web form data
Dim key As String = TryCast(Page.Request.Params(VIEW_STATE_CACHEKEY), String)
'Abort if cache key is not available or valid
If String.IsNullOrEmpty(key) Or Not key.StartsWith("VS_") Then
Throw New ApplicationException("Missing valid " + VIEW_STATE_CACHEKEY)
End If
Dim state As Pair = TryCast(DataCache.GetPersistentCacheItem(key, GetType(Pair)), Pair)
If Not state Is Nothing Then
'Set view state and control state
ViewState = state.First
ControlState = state.Second
End If
'Remove this ViewState from the cache as it has served its purpose
DataCache.RemovePersistentCacheItem(key)
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' Saves the Page State to the Cache
''' </summary>
''' <history>
''' [cnurse] 11/30/2006 Documented
''' </history>
''' -----------------------------------------------------------------------------
Public Overrides Sub Save()
'No processing needed if no states available
If ViewState Is Nothing And ControlState Is Nothing Then
Exit Sub
End If
'Generate a unique cache key
Dim key As New StringBuilder()
With key
.Append("VS_")
.Append(IIf(Page.Session Is Nothing, Guid.NewGuid().ToString(), Page.Session.SessionID))
.Append("_")
.Append(DateTime.Now.Ticks.ToString())
End With
'Save view state and control state separately
Dim state As New Pair(ViewState, ControlState)
'Add view state and control state to cache
DataCache.SetCache(key.ToString(), state, Nothing, DateTime.Now.AddMinutes(Page.Session.Timeout), System.Web.Caching.Cache.NoSlidingExpiration, True)
'Register hidden field to store cache key in
Page.ClientScript.RegisterHiddenField(VIEW_STATE_CACHEKEY, key.ToString())
End Sub
End Class
End Namespace