【问题标题】:Which Session Method to use when removing?删除时使用哪种会话方法?
【发布时间】:2011-03-17 04:50:55
【问题描述】:

我一直在使用 Session.clear() 并且我在 firecookie 中注意到该会话仍然存在。所以我开始四处搜索,发现有 4 种方法可以删除会话

Session.Remove(strSessionName);      Remove an Item from Session State Collection
Session.RemoveAll()     Remove all items from session collection 
Session.Clear()     Remove all items from session collection  Note: There is no difference between Clear and RemoveAll. RemoveAll() calls Clear(), internally.
Session.Abandon()   Cancels the Current Session

现在 clear 和 remove 似乎做同样的事情,但你应该使用哪一个,比如为什么使用 remove() 而不是放弃而不是 clear。

如果你使用 session.Abandon 它会杀死当前会话。 clear() 删除值的位置。

为什么您只想保留没有值的会话?为什么不把它彻底杀死?

【问题讨论】:

    标签: c# .net asp.net


    【解决方案1】:

    Session.Abandon() 会触发 global.asax 文件中的 Session_End 事件,而 Session.Clear() 不会。

    设想一个场景,您存储用户的会话开始时间和结束时间以用于审计目的 - 您可能希望偶尔清除他们的会话(例如更新配置文件设置),而不触发 Session_End 事件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-21
      • 2015-12-13
      • 2012-10-22
      • 1970-01-01
      • 2011-01-22
      相关资源
      最近更新 更多