Session.Abandon-Session.Clear-Session.RemoveAll

System.Web.UI.Page.Session属性和System.Web.HttpContext.Session属性 都是System.Web.SessionState.HttpSessionState类

这三种方法都可以清除session.

Session.Clear()和Session.RemoveAll()是完全一样的,即Clears all values from the session-state item collection.

Session.RemoveAll()内部就是调用了Session.Clear()

源码

// System.Web.SessionState.HttpSessionState
/// <summary>Removes all keys and values from the session-state collection.</summary>
public void RemoveAll()
{
    this.Clear();
}

以上两种方法和Session.Abandon()是有区别的。

以上两种方法,只是清空了键值集合,而Abandon是Ends the current session.意味着Session对象被销毁destroyed,当然其保存的键值集合也会同样消失...

 InProc模式下abandon方法会引发Session_End事件

Once the Abandon method is called, the current session is no longer valid and a new session can be started. Abandon causes the End event to be raised. A new Start event will be raised on the next request.

 

Abandon方法调用后,不是马上销毁,而是等待剩下的脚本执行完毕,因此仍在在随后的代码中被访问,但是本次请求完毕,随后的请求就无法继续访问到了

When the Abandon method is called, the current Session object is queued for deletion but is not actually deleted until all of the script commands on the current page have been processed. This means that you can access variables stored in the Session object on the same page as the call to theAbandon method but not in any subsequent Web pages.

 

<% 
  Session.Abandon  
  Session("MyName") = "Mary" 
  Reponse.Write(Session("MyName")) //本次请求仍然有值
%> 

 

新的请求将会创建新的Session对象,虽然仍然存储相同的键值,例如

Session("MyName") = "Mary" ;

,但是这个Session已经是新对象了

 

 

另外,如果inproc模式下,设置的时间超时,会自动调用Abandon方法

The Abandon method destroys all the objects stored in a Session object and releases their resources. If you do not call the Abandonmethod explicitly, the server destroys these objects when the session times out.

https://msdn.microsoft.com/en-us/library/ms524310(v=vs.90).aspx

转载于:https://www.cnblogs.com/imust2008/p/5509896.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值