使用 HttpModule 将事件附加到会话 OnStart 和 OnEnd

发布于 2024-07-13 22:34:25 字数 80 浏览 5 评论 0原文

有谁知道使用 HttpModule 将事件添加到会话的 OnStart 和 OnEnd 事件的干净方法(无需触及 Global.asax 文件)?

Does anyone know a clean way of adding events to Session's OnStart and OnEnd events using an HttpModule (without touching the Global.asax file)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场春暖 2024-07-20 22:34:25
public void Init(HttpApplication app)
{
   var ssm = app.Modules["Session"] as SessionStateModule;
   ssm.Start += Foo;
   ssm.End += Bar;
}
public void Init(HttpApplication app)
{
   var ssm = app.Modules["Session"] as SessionStateModule;
   ssm.Start += Foo;
   ssm.End += Bar;
}
尹雨沫 2024-07-20 22:34:25

可以通过在 HttpModule 的请求事件之一中检查 HttpContext.Current.Session.IsNewSession 是否设置为 true 来模拟会话 OnStart 行为。

然而,有一个陷阱! 如果未在 Session 对象中设置值,则下一个请求将具有正值 IsNewSession。 因此,一旦您积极检查 IsNewSession,您应该在 Session 对象中设置任何值。

Session OnStart behavior can be emulated by - in one of your HttpModule´s request events - checking if HttpContext.Current.Session.IsNewSession is set to true.

However there's one pitfall! If not a value is set in the Session object, the next request will have a positive value as IsNewSession. So once you possitively checked for IsNewSession you should set any value in a Session object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文