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

posted @ 2016-05-19 19:50  悠哉大斌  阅读(307)  评论(0编辑  收藏  举报