Asp.net 2.0 ViewState原理

ViewState相信大家都会使用,可ViewState到底是什么,又有多少人知道呢?

StateBag类这个就不用多说啦吧

在Asp.net 2.0 里,用到StateBag有三处

1 Control._viewState  这个就是大家使用的ViewState

2 WebControl.attrState这个是存放Attribute的

3 Style.statebag是存放样式的
.......
Page生命周期内SaveAllState时

需要先生成个Piar类,在调用this.SavePageStateToPersistenceMedium(pair1);时,将其序列化

注意:Asp.net2.0只实现了HiddenFieldPageStatePersister,用户可以从重写,或者使用ControlAdapter提供其它形式的进理机制

HiddenFieldPageStatePersister.Save时会过pair1进行序列化

序列化时,.net提供了三种方式

1使用密钥

2.使用Mac

3不使用

  //先序列化
  this.Serialize(outputStream, stateGraph);

 outputStream.SetLength(outputStream.Position);
 byte[] buf = outputStream.GetBuffer();

  int length = (int)outputStream.Length;
          
//判断当前Page.RequiresViewStateEncryptionInternal属性返回值是不是需要加密            
//如果未调用Page.RegisterRequiresViewStateEncryption,则默认为false               
//如果界面设置了RegisterRequiresViewStateEncryption和EnableViewStateMac,加密优先于Mac
               
if ((this._page != null) && this._page.RequiresViewStateEncryptionInternal)    //加密               
{                   
    buf = MachineKeySection.EncryptOrDecryptData(true, buf, this.GetMacKeyModifier(), 0, length);                   
    length = buf.Length;              
}              
else if (((this._page != null) && this._page.EnableViewStateMac) || (this._macKeyBytes != null))//设置可以使用Mac               
{                   
    buf = MachineKeySection.GetEncodedData(buf, this.GetMacKeyModifier(), 0, ref length);               
}

text = Convert.ToBase64String(buf, 0, length); // null of either

谈到这,很多人要问pair1里放的是什么,我画了一幅图,详细说明了一下


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值