怎么去除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); }
第二种方法不知道session会不会丢失,如果丢失了肯定会有影响,怎么办??
protected LosFormatter losFormatter = new LosFormatter();
protected override object LoadPageStateFromPersistenceMedium() { string key = Request.RawUrl + "__viewstate";
if (Session[key] != null) { object obj = null; using (System.IO.MemoryStream stream = (System.IO.MemoryStream)Session[key]) { stream.Seek(0, System.IO.SeekOrigin.Begin); obj = losFormatter.Deserialize(stream); } Session[key] = null; return obj; }
return null; }
protected override void SavePageStateToPersistenceMedium(object viewState) { string key = Request.RawUrl + "__viewstate"; System.IO.MemoryStream stream = new System.IO.MemoryStream(); losFormatter.Serialize(stream, viewState); stream.Flush(); Session[key] = stream;
}
其他更好的解决办法??
- 12 回答
- 0 关注
- 401 浏览
添加回答
举报
0/150
提交
取消