Session.Clear()与Session.RemoveAll()以及Session.Abandon()有什么区别?
2009-07-30 17:07 Johnny Qian 阅读(3048) 评论(1) 编辑 收藏 举报首先给出微软在HttpSessionState类中两个方法的实现:
public void Clear()
{
_dict.Clear();
}
public void RemoveAll()
{
Clear();
}
RemoveAll 方法调用了 Clear 方法。
msdn对三个函数的描述是:
HttpSessionState.Clear :Removes all keys and values from the session-state collection.
HttpSessionState.RemoveAll:Removes all keys and values from the session-state collection.
HttpSessionState.Abandon :Cancels the current session.
以下摘自 论坛:http://forums.asp.net/
Both do the same thing - remove all entries from the session state collection.
There is no difference. RemoveAll just calls Clear. So it is better to call Clear instead of RemoveAll.
另一个论坛:
Is there a difference between Session.Clear() and Session.RemoveAll()? The
descriptions and documentation pages seem to say exactly the same thing, but
I am assuming there must be some reason for creating two functions, right?
Thanks.
--
Nathan Sokalski
You are correc that they do the same exact thing. And that is because
..RemoveAll calls .Clear. So, why do they both exist? Got me! Perhaps it
was just a lack of communication when the MS employees were developing
asp.net.
Ray at home
又一个论坛:
What is the best way to end session?
Hi all,
I noticed that there are three methods that can remove session
variables
Session.Clear()
Session.RemoveAll()
Session.Abandon()
Now my question as is stated on the subject, what is the best way to
end a session?
I know that the name itself is telling me that Session.Abandon() should
be the methond called when ending a session, but if the other two
methods clear or remove all the variables, isn't that the same, since
if I want to go to a page that requires a session variable it just
won't have it?
Actually I found using either RemoveAll or Clear, better than Abandon,
since the Remarks on the Abandon method on MSDN already state that you
can still access session variables after calling the method.
Could anybody explain the difference among these methods?
Re: What is the best way to end session?
The first 2 only remove session variables you have stored so far. The
session itself is still alive.
Session.Abandon is the only one of those that actually kills the session.
"Hugo Flores" <hugo.flores [at] ge.com> wrote in message
http://www.cnblogs.com/moneyriver2006/admin/news:1121360161.953896.131270 [at] g47g2000cwa.googlegroups.com.. .
Marina [ Do, 14 Juli 2005 19:09 ] [ ID #878685 ]
Re: What is the best way to end session?
Clear() and RemoveAll() perform the same thing: remove the session variables
but keep the current session in memory. Whereas, Abandon() ends the current
session.
"Hugo Flores" <hugo.flores [at] ge.com> wrote in message
http://www.cnblogs.com/moneyriver2006/admin/news:1121360161.953896.131270 [at] g47g2000cwa.googlegroups.com.. .
因此,在退出登录时我的一般写法是:
Session.Clear();
Session.Abandon();
本文基于知识共享署名 2.5 中国大陆许可协议发布,欢迎转载、演绎或用于商业目的,但是必须保留本文的署名Johnny Qian和链接。