珠海SEO 陈亮的网络营销博客:微网站_微营销

VIEWSTATE过大的解决方法测试中

环保清洗网里的部分程序代码还有待完善,特别是每个页面里的VIEWSTATE的参数都很多,有的甚至达到了30K左右,对网页的用户体验度有一定的影响,今天开始尝试解决这个问题,在CSDN里有学长提出解决办法:
楼主的代码我在asp.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


======================
 

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();
            }
        }

不错测试后出错:

Compiler Error Message: CS1518: Expected class, delegate, enum, interface, or struct

Source Error:
Line 14: using System.Threading;
Line 15: #region 解决ViewState过于庞大的问题
Line 16:     protected override object LoadPageStateFromPersistenceMedium()
Line 17:     {
Line 18:         string viewStateID = (string)((Pair)base.LoadPageStateFromPersistenceMedium()).Second;

Source File: f:\Web\ProductBuy\Index.aspx.cs    Line: 16
问题有待进一步研究!如果有解决了的朋友希望指点,我这里是.net   2.0

Tags: VIEWSTATE过大

发布: 陈亮 分类: 电脑应用 评论: 0 浏览: 242
留言列表
发表留言
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。