状态

状态

  由于HTTP协议的无状态性,为了解决无状态ASP.NET提供了以下的几种方案

  1. 隐藏域
    1.   隐藏域呢,就是一个<input type="hidden"/>的一个标签,由于他会在提交表单的时候自动附加到请求上面去,所有它可以用来保存状态
    2.      可是如果客户端断开了连接就没有办法保存信息了,所有它适合短时间的保存
  2. Cookie  (以请求头的形式附加上去)
    1.  格式name=value Set-Cookie:message=hello,world.;
    2. 在ASP.NET中HttpCookie配合Response.Cookie来设置回应的Cookie可以通过设置Expires属性来设置过期的属性

    3. Domain 可以限制Cookie发送的域

    4. Path 需要和请求的虚拟路径的开头匹配

    5. 当然由于Cookie的长度和浏览器的限制,它只能用来保存比较小的数据
  3. URL
  4. Session  回话
    1. 原理:在服务端保存,然后把key发送到客户端,然后请求的时候一起发送过来这个key一般以Cookie的方式保存,或者URL
    2. 如果要在一般处理程序中使用Session需要实现接口IRequiresSessionState

      1. 压缩Session数据
        <sessionState
          mode="SqlServer"
        SqlConnectionString="data source="
        allowCustomSqlDatabase="true"
        compressionEnabled="true"/>

  5. ViewState 视图状态
    1.   序列化

      public interface IStateManager
      {
        //表示控件是否使用视图状态
        bool IsTrackingViewState{get;}
        //管理视图状态的方法
        void LoadViewState(Object state);//从视图状态取回数据
        Object SaveViewState();//保存视图状态
        void TrackViewState();//开始使用视图状态
      }
    2.  页面视图状态保存和恢复的时间点
      保存:
        1.PreRender
        2.SaveViewState
        3.SavePageStateToPersistenceMedium
        4.Page_SaveStateComplete
      恢复:
        1.InitComplete
        2.LoadPageStateFromPersistenceMediu
        3.LoadViewState
        4.PreLoad
    控件状态 ControlState
    protected override void OnInit(EventArgs e)
    {
      base.OnInit(e);
      Page.RegisterRequiresControlState(this);
    }

 

  1. Cache 

    1. CacheDependency

 

      SqlDependency

      过期时间
        绝对过期时间
        滑动过期时间
      优先级别 CacheItemPriority
        NotRemovable
        High
        AboveNormal
        Normal
        Default
        BelowNormal
        Low
      删除通知
        public delegate void CacheItemRemovedCallback
        (
          string key,
          Object value,
          CacheItemRemovedReason reason//被移出的原因,枚举类型
        )

    部分缓存

      1.用户控件
      2.缓存后替换 通过设置他们的方法,当方法必须是线程安全的,并返回字符串
        Substitution控件
        AdRotator控件
      自定义的输出缓存提供器 System.Web.Caching.OutputCacheProvider;
        实现自定义的输出缓存提供其,必须实现上面的4个方法,然后再配置中进行配置
        <caching>
          <outputCache defaultProvider="AspNetInternalProvider">
            <providers>
        <add name="DiskCache"
          type="Test.OutputCaheEx.DiskOutputCacheProvider,DiskCacheProvider"
        />
            </providers>
          </outputCache>
      </caching>
    在UserControl中是也可以选择缓存管理提供器providerName="name"
    如果需要为页面配置缓存管理提供器需要在Global.asax中实现一个特殊的方法GetOutputCacheProviderName
    publi override string GetOutputCacheProviderName(HttpContext context)
    {
      if(context.Request.Path.EndsWith("Advanced.aspx"))
        return "DiskCache";
      else
        return base.GetOutputCacheProviderName(context);
    }

 

HttpApplication

posted @ 2016-12-08 14:42  夏风微凉  阅读(243)  评论(0编辑  收藏  举报