• ViewState保存在服务器,可定时清空


    通过csdn中sp1234的帖子进行整理

    ViewstateMethod.cs

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.IO;
    using System.Web.UI;
    using System.Threading;


    /// <summary>
    ///ViewstateMethod 的摘要说明
    /// </summary>

    namespace Lear
    {
    public class ViewstateMethod : System.Web.UI.Page
    {
    #region 解决ViewState过于庞大的问题
    protected override object LoadPageStateFromPersistenceMedium()
    {
    string viewStateID = (string)((Pair)base.LoadPageStateFromPersistenceMedium()).Second;
    string stateObject = (string)Cache[viewStateID];
    if (stateObject != null)
    {
    Cache.Remove(viewStateID);
    return stateObject;
    }

    string fn = Path.Combine(this.Request.PhysicalApplicationPath, @"App_Data/ViewState/" + viewStateID);
    string stateStr = File.ReadAllText(fn);
    return new ObjectStateFormatter().Deserialize(stateStr);
    }

    protected override void SavePageStateToPersistenceMedium(object state)
    {
    string viewStateID = (DateTime.Now.Ticks + (long)this.GetHashCode()).ToString(); //产生离散的id号码
    Cache.Insert(viewStateID, state);
    //ThreadPool.QueueUserWorkItem(obj =>
    //{
    string value = new ObjectStateFormatter().Serialize(state);
    string fn = Path.Combine(this.Request.PhysicalApplicationPath, @"App_Data/ViewState/" + viewStateID);
    File.WriteAllText(fn, value);
    //});
    base.SavePageStateToPersistenceMedium(viewStateID);
    }
    #endregion
    }
    }

    定时删除Viewstate文件方法,在Page_Load中调用,时间自行调整,建议放在root.master.cs文件中(最外层的模版)

    View Code
        #region 删除ViewState中过时的信息
    private void DelViewstateForApplication()
    {
    if (Application["delDatetime"] != null)
    {
    DateTime thisNow = System.DateTime.Now;
    DateTime lastNow = thisNow;
    DateTime delDatetime = Convert.ToDateTime(Application["delDatetime"]);
    if ((thisNow - delDatetime).Minutes > 1)//时间自行调整
    {
    lock (Application)
    {
    if (delDatetime == Convert.ToDateTime(Application["delDatetime"]))
    {
    lastNow = Convert.ToDateTime(Application["delDatetime"]);
    Application["delDatetime"] = System.DateTime.Now;
    }
    }
    }
    if (thisNow != lastNow)
    DelViewStateData();
    }
    else
    {
    bool first = false;
    lock (Application)
    {
    if (Application["delDatetime"] == null)
    {
    Application["delDatetime"] = System.DateTime.Now;
    first = true;
    }
    }
    if (first)
    DelViewStateData();
    }
    }

    private void DelViewStateData()
    {
    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(this.Server.MapPath("~/App_Data/ViewState/"));
    if (!dir.Exists)
    dir.Create();
    else
    {
    DateTime nt = DateTime.Now.AddMinutes(-1);//时间自行调整
    foreach (System.IO.FileInfo f in dir.GetFiles())
    {
    if (f.CreationTime < nt)
    f.Delete();
    }
    }
    }
    #endregion

    最终aspx页面使用方法:如default.aspx.cs文件中的类需要继承自Lear.ViewstateMethod,而不是System.Web.UI.Page

    没有交互提交的页面,可在头部Page中加入EnableViewState="false"以减小数据大小,直接继承System.Web.UI.Page,而不继承自Lear.ViewstateMethod生成多余的Viewstate文件。

    正常的页面可以关掉EnableViewState="false" EnableViewStateMac="false" EnableEventValidation="false"

    注:本方法只是个人总结,请慎重使用。

  • 相关阅读:
    Windows 服务的安装(1)
    C#编写window服务,一步一步(1)
    .net 读写记事本文件
    C#中POST数据和接收的几种方式(抛砖引玉)
    Newtonsoft.Json 通过 JObject 读取 json对像 超简单
    Winform读写App.config文件以及重启程序
    WebMatrix之WebMatrix.Data
    C#中方法的参数的四种类型
    zh-Hans vs.net 通过 管理nuget程序包下载简体中文语言包 zh-cn
    .net C# 图片转Base64 Base64转图片
  • 原文地址:https://www.cnblogs.com/lear/p/2374843.html
Copyright © 2020-2023  润新知