/* from: 没有问题,散分贴:如果你必须开发交互式页面,ViewState越大可能意味着越有本事 */
原作者sp1234 .NET3.0代码
如果你有一个非常酷的页面,页面上很多东西自动地响应用户操作而展现丰富的变化,你的ViewState是很有可能达到200K的。
这里是我将ViewState持久化保持在服务器端的代码,这样ViewState不占用网络带宽,因此其存取只是服务器的磁盘读取时间。并且它很小,可以说是磁盘随便转一圈就能同时读取好多ViewState,因此可以说“不占时间”。为了再“不占磁盘时间”,我还使用了缓存。
一下这段代码可以放在页面中,或者页面的父类中:
protected override object LoadPageStateFromPersistenceMedium()
{
var viewStateID = (string)((Pair)base.LoadPageStateFromPersistenceMedium()).Second;
var stateStr = (string)Cache[viewStateID];
if (stateStr == null)
{
var fn = this.Server.MapPath(@"~/App_Data/ViewState/" + viewStateID); stateStr = File.ReadAllText(fn);
}
return new ObjectStateFormatter().Deserialize(stateStr);
}
protected override void SavePageStateToPersistenceMedium(object state)
{
var value = new ObjectStateFormatter().Serialize(state);
var viewStateID = (DateTime.Now.Ticks + (long)this.GetHashCode()).ToString(); //产生离散的id号码
var fn = this.Server.MapPath(@"~/App_Data/ViewState/" + viewStateID); <br /> //ThreadPool.QueueUserWorkItem(obj => File.WriteAllText(fn, value));
File.WriteAllText(fn, value);
Cache.Insert(viewStateID, value);
base.SavePageStateToPersistenceMedium(viewStateID);
}
{
var viewStateID = (string)((Pair)base.LoadPageStateFromPersistenceMedium()).Second;
var stateStr = (string)Cache[viewStateID];
if (stateStr == null)
{
var fn = this.Server.MapPath(@"~/App_Data/ViewState/" + viewStateID); stateStr = File.ReadAllText(fn);
}
return new ObjectStateFormatter().Deserialize(stateStr);
}
protected override void SavePageStateToPersistenceMedium(object state)
{
var value = new ObjectStateFormatter().Serialize(state);
var viewStateID = (DateTime.Now.Ticks + (long)this.GetHashCode()).ToString(); //产生离散的id号码
var fn = this.Server.MapPath(@"~/App_Data/ViewState/" + viewStateID); <br /> //ThreadPool.QueueUserWorkItem(obj => File.WriteAllText(fn, value));
File.WriteAllText(fn, value);
Cache.Insert(viewStateID, value);
base.SavePageStateToPersistenceMedium(viewStateID);
}
不使用Session,因为它会“丢失”。ViewState保存在磁盘上,即使服务器重新启动,也不会丢失页面状态。
下面这段可以放在Global.asax中,也可以根本不管:
protected void Application_Start(object sender, EventArgs e)
{
var dir = new DirectoryInfo(this.Server.MapPath("~/App_Data/ViewState/"));
if (!dir.Exists)
dir.Create();
else
{
var nt = DateTime.Now.AddHours(-1);
dir.GetFiles().ForEach(f =>
{
if (f.CreationTime < nt)
f.Delete();
});
}
}
{
var dir = new DirectoryInfo(this.Server.MapPath("~/App_Data/ViewState/"));
if (!dir.Exists)
dir.Create();
else
{
var nt = DateTime.Now.AddHours(-1);
dir.GetFiles().ForEach(f =>
{
if (f.CreationTime < nt)
f.Delete();
});
}
}
46539492修改的.NET2.0代码
#region 解决ViewState过于庞大的问题
protected override object LoadPageStateFromPersistenceMedium()
{
string viewStateID = (string)((Pair)base.LoadPageStateFromPersistenceMedium()).Second;
string stateStr = (string)Cache[viewStateID];
if (stateStr == null)
{
string fn = Path.Combine(this.Request.PhysicalApplicationPath, @"App_Data/ViewState/" + viewStateID);
stateStr = File.ReadAllText(fn);
}
return new ObjectStateFormatter().Deserialize(stateStr);
}
protected override void SavePageStateToPersistenceMedium(object state)
{
string value = new ObjectStateFormatter().Serialize(state);
string viewStateID = (DateTime.Now.Ticks + (long)this.GetHashCode()).ToString(); //产生离散的id号码
string fn = Path.Combine(this.Request.PhysicalApplicationPath, @"App_Data/ViewState/" + viewStateID);
//ThreadPool.QueueUserWorkItem(File.WriteAllText(fn, value));
File.WriteAllText(fn, value);
Cache.Insert(viewStateID, value);
base.SavePageStateToPersistenceMedium(viewStateID);
}
#endregion
protected override object LoadPageStateFromPersistenceMedium()
{
string viewStateID = (string)((Pair)base.LoadPageStateFromPersistenceMedium()).Second;
string stateStr = (string)Cache[viewStateID];
if (stateStr == null)
{
string fn = Path.Combine(this.Request.PhysicalApplicationPath, @"App_Data/ViewState/" + viewStateID);
stateStr = File.ReadAllText(fn);
}
return new ObjectStateFormatter().Deserialize(stateStr);
}
protected override void SavePageStateToPersistenceMedium(object state)
{
string value = new ObjectStateFormatter().Serialize(state);
string viewStateID = (DateTime.Now.Ticks + (long)this.GetHashCode()).ToString(); //产生离散的id号码
string fn = Path.Combine(this.Request.PhysicalApplicationPath, @"App_Data/ViewState/" + viewStateID);
//ThreadPool.QueueUserWorkItem(File.WriteAllText(fn, value));
File.WriteAllText(fn, value);
Cache.Insert(viewStateID, value);
base.SavePageStateToPersistenceMedium(viewStateID);
}
#endregion
DirectoryInfo dir = new DirectoryInfo(this.Server.MapPath("~/App_Data/ViewState/"));
if (!dir.Exists)
dir.Create();
else
{
DateTime nt = DateTime.Now.AddHours(-1);
foreach(FileInfo f in dir.GetFiles())
{
if (f.CreationTime < nt)
f.Delete();
}
}
if (!dir.Exists)
dir.Create();
else
{
DateTime nt = DateTime.Now.AddHours(-1);
foreach(FileInfo f in dir.GetFiles())
{
if (f.CreationTime < nt)
f.Delete();
}
}