牛B的处理ViewState

    void Application_Start(object sender, EventArgs e)
    {
        //在应用程序启动时运行的代码

        var dir = new System.IO.DirectoryInfo(this.Server.MapPath("~/App_Data/ViewState/"));
        if (!dir.Exists)
        {
            dir.Create();
        }
        else
        {
            var dt = DateTime.Now.AddHours(-1);
            foreach (System.IO.FileInfo fi in dir.GetFiles())
            {
                if (fi.CreationTime < dt)
                {
                    fi.Delete();
                }
            }
        }
    }

 

 

        /// <summary>
        /// ViewState的相关处理
        /// </summary>
        /// <returns></returns>
        protected override object LoadPageStateFromPersistenceMedium()
        {
            var viewStateID = (string)((Pair)base.LoadPageStateFromPersistenceMedium()).Second;
            var stateObject = Cache[viewStateID];
            if (stateObject != null)
            {
                return stateObject;
            }

            var fn = this.Server.MapPath("~/App_Data/ViewState/" + viewStateID);
            var stateStr = System.IO.File.ReadAllText(fn);
            return new ObjectStateFormatter().Deserialize(stateStr);
        }

        protected override void SavePageStateToPersistenceMedium(object state)
        {
            var viewStateID = (DateTime.Now.Ticks + this.GetHashCode()).ToString(); //产生离散的id号码
            Cache.Insert(viewStateID, state);
            var value = new ObjectStateFormatter().Serialize(state);

            var fn = this.Server.MapPath("~/App_Data/ViewState/" + viewStateID);
            System.Threading.ThreadPool.QueueUserWorkItem(obj => System.IO.File.WriteAllText(fn, value));
            base.SavePageStateToPersistenceMedium(viewStateID);
        }

你可能感兴趣的:(object,cache,String,null,application,pair)