Created
January 8, 2018 18:01
-
-
Save Jalalx/3d4ffaee3c95e1cc7f4d36ef23040ba9 to your computer and use it in GitHub Desktop.
Save view state in ASP.NET Cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From: https://weblogs.asp.net/ricardoperes/saving-view-state-in-cache | |
// Override following property in your pages. | |
// protected override PageStatePersister PageStatePersister | |
// { | |
// get | |
// { | |
// return (new CachePageStatePersister(this)); | |
// } | |
// } | |
public class CachePageStatePersister : PageStatePersister | |
{ | |
private const String RequestId = "__REQUESTID"; | |
private const String ViewStateId = "VIEWSTATE:{0}"; | |
private const String ControlStateId = "CONTROLSTATE:{0}"; | |
private const String CacheTimeoutMinutesKey = "CacheTimeoutMinutes"; | |
private const Int32 DefaultCacheTimeoutMinutes = 10; | |
public CachePageStatePersister(Page page): base(page) | |
{ | |
} | |
public override void Load() | |
{ | |
String id = this.Page.ID; | |
if ((String.IsNullOrWhiteSpace(id) == true) || (id == "__Page")) | |
{ | |
id = this.Page.Request.Form[RequestId]; | |
} | |
if (String.IsNullOrWhiteSpace(id) == true) | |
{ | |
throw (new InvalidOperationException("Missing page id")); | |
} | |
this.Page.ID = id; | |
this.Page.ClientScript.RegisterHiddenField(RequestId, id); | |
String viewStateId = String.Format(ViewStateId, id); | |
String controlStateId = String.Format(ControlStateId, id); | |
this.ViewState = this.Page.Cache[viewStateId]; | |
this.ControlState = this.Page.Cache[controlStateId]; | |
} | |
public override void Save() | |
{ | |
if ((this.ControlState != null) || (this.ViewState != null)) | |
{ | |
String id = this.Page.ID; | |
if ((String.IsNullOrWhiteSpace(id) == true) || (id == "__Page")) | |
{ | |
id = Guid.NewGuid().ToString(); | |
this.Page.ID = id; | |
this.Page.ClientScript.RegisterHiddenField(RequestId, id); | |
} | |
Int32 cacheTimeoutMinutes = DefaultCacheTimeoutMinutes; | |
if (String.IsNullOrWhiteSpace(ConfigurationManager.AppSettings[CacheTimeoutMinutesKey]) == false) | |
{ | |
Int32.TryParse(ConfigurationManager.AppSettings[CacheTimeoutMinutesKey], out cacheTimeoutMinutes); | |
} | |
if (this.Page.Items[CacheTimeoutMinutesKey] is Int32) | |
{ | |
cacheTimeoutMinutes = (Int32)this.Page.Items[CacheTimeoutMinutesKey]; | |
} | |
String viewStateId = String.Format(ViewStateId, id); | |
String controlStateId = String.Format(ControlStateId, id); | |
if (this.ViewState != null) | |
{ | |
this.Page.Cache.Add(viewStateId, this.ViewState, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(cacheTimeoutMinutes), System.Web.Caching.CacheItemPriority.Default, null); | |
} | |
if (this.ControlState != null) | |
{ | |
this.Page.Cache.Add(controlStateId, this.ControlState, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(cacheTimeoutMinutes), System.Web.Caching.CacheItemPriority.Default, null); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment