如果你有一个非常酷的页面,页面上很多东西自动地响应用户操作而展现丰富的变化,你的ViewState是很有可能达到200K的。
这里是我将ViewState持久化保持在服务器端的代码,这样ViewState不占用网络带宽,因此其存取只是服务器的磁盘读取时间。并且它很小,可以说是磁盘随便转一圈就能同时读取好多ViewState,因此可以说“不占时间”。为了再“不占磁盘时间”,我还使用了缓存。
一下这段代码可以放在页面中,或者页面的父类中:
1 protected override object LoadPageStateFromPersistenceMedium()
2 {
3 var viewStateID = (string)((Pair)base.LoadPageStateFromPersistenceMedium()).Second;
4 var stateStr = (string)Cache[viewStateID];
5 if (stateStr == null)
6 {
7 var fn = Path.Combine(this.Request.PhysicalApplicationPath, @"App_Data/ViewState/" + viewStateID);
8 stateStr = File.ReadAllText(fn);
9 }
10 return new ObjectStateFormatter().Deserialize(stateStr);
11 }
12
13 protected override void SavePageStateToPersistenceMedium(object state)
14 {
15 var value = new ObjectStateFormatter().Serialize(state);
16 var viewStateID = (DateTime.Now.Ticks + (long)this.GetHashCode()).ToString(); //产生离散的id号码
17 var fn = Path.Combine(this.Request.PhysicalApplicationPath, @"App_Data/ViewState/" + viewStateID);
18 ThreadPool.QueueUserWorkItem(obj => File.WriteAllText(fn, value));
19 Cache.Insert(viewStateID, value);
20 base.SavePageStateToPersistenceMedium(viewStateID);
21 }
3 var viewStateID = (string)((Pair)base.LoadPageStateFromPersistenceMedium()).Second;
4 var stateStr = (string)Cache[viewStateID];
5 if (stateStr == null)
6 {
7 var fn = Path.Combine(this.Request.PhysicalApplicationPath, @"App_Data/ViewState/" + viewStateID);
8 stateStr = File.ReadAllText(fn);
9 }
10 return new ObjectStateFormatter().Deserialize(stateStr);
11 }
12
13 protected override void SavePageStateToPersistenceMedium(object state)
14 {
15 var value = new ObjectStateFormatter().Serialize(state);
16 var viewStateID = (DateTime.Now.Ticks + (long)this.GetHashCode()).ToString(); //产生离散的id号码
17 var fn = Path.Combine(this.Request.PhysicalApplicationPath, @"App_Data/ViewState/" + viewStateID);
18 ThreadPool.QueueUserWorkItem(obj => File.WriteAllText(fn, value));
19 Cache.Insert(viewStateID, value);
20 base.SavePageStateToPersistenceMedium(viewStateID);
21 }
不使用Session,因为它会“丢失”。ViewState保存在磁盘上,即使服务器重新启动,也不会丢失页面状态。
下面这段可以放在Global.asax中,也可以根本不管:
1 protected void Application_Start(object sender, EventArgs e)
2 {
3 var dir = new DirectoryInfo(this.Server.MapPath("~/App_Data/ViewState/"));
4 if (!dir.Exists)
5 dir.Create();
6 else
7 {
8 var nt = DateTime.Now.AddHours(-1);
9 dir.GetFiles().ForEach(f =>
10 {
11 if (f.CreationTime < nt)
12 f.Delete();
13 });
14 }
15 }
2 {
3 var dir = new DirectoryInfo(this.Server.MapPath("~/App_Data/ViewState/"));
4 if (!dir.Exists)
5 dir.Create();
6 else
7 {
8 var nt = DateTime.Now.AddHours(-1);
9 dir.GetFiles().ForEach(f =>
10 {
11 if (f.CreationTime < nt)
12 f.Delete();
13 });
14 }
15 }
这可以确保绝对稳定可靠地工作。以后请放心使用ViewState,把交互式页面提高水平才是最重要的,不要纠缠在“ViewState太大”上。实际上,由于页面设计不够酷,交互变化看上去不够丰富,ViewState实在是太小太小了。
[注:此方法有利有弊,仅供参考!]