使用 Javascript 处理 ASP.NET 中的会话超时

发布于 2024-08-16 05:52:35 字数 894 浏览 8 评论 0 原文

本质上,我希望能够捕获用户让其会话超时,然后单击最终导致异步回发的内容。我发现,如果我将此代码放入我的 Session_Start (在 Global.asax 中),那么我可以捕获会话超时期间发生的回发:

    With HttpContext.Current
        If TypeOf .Handler Is Page Then
            Dim page As Page = CType(.Handler, Page)
            If page IsNot Nothing AndAlso page.IsPostBack Then
                'Session timeout
            End If
        End If
    End With

这工作正常。我的问题是,我希望能够将一些 JavaScript 注入到 Response 中,然后调用 Response.End() ,以便应用程序的其余部分不会完成执行。问题是,当我尝试 Response.Write("

总结一下:我需要将 javascript 注入 Global.asax 中 Session_Start 事件的响应中

注意:您可能想知道为什么我不在 Session_End 中执行此操作...我们不使用 InProc 会话,因此 Session_End 也不使用不会被调用...但这不是重点...只是想弄清楚为什么我在 Session_Start 中这样做。

Essentially I want to be able to catch when a user lets their session timeout and then clicks on something that ends up causing an Async postback. I figured out that if I put this code in my Session_Start (in Global.asax) then I can catch a postback that occurred during a session timeout:

    With HttpContext.Current
        If TypeOf .Handler Is Page Then
            Dim page As Page = CType(.Handler, Page)
            If page IsNot Nothing AndAlso page.IsPostBack Then
                'Session timeout
            End If
        End If
    End With

This works fine. My question is, I would like to be able to inject some javascript into the Response and then call Response.End() so that the rest of the application does not get finish executing. The problem is that when I try Response.Write("<script ... ") followed by Response.End() then javascript does not get written to the response stream. I'm sure there are other places in the application that I can safely write Javascript to the Response but I can't let the rest of the application execute because it will error when it tries to access the session objects.

To sum up: I need to inject javascript into the response in the Session_Start event in Global.asax

Note: You may be wondering why I'm not doing this in Session_End...we don't use InProc sessions and so Session_End doesn't get called...but that's beside the point...just wanted to make it clear why I'm doing this in Session_Start.

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

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

发布评论

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

评论(1

丘比特射中我 2024-08-23 05:52:35

在 HttpHandler 外部写入响应流通常不是一个好主意;它可能在某些极端情况下有效,但这并不是事情的预期工作方式。

您是否考虑过使用页面基类或页面适配器来执行此操作?这样,您只需要一份代码副本,并且它可以应用于所有页面或仅应用于您选择的页面。

另一种选择是使用 URL 重写将传入请求重定向到生成所需脚本输出的页面。

Writing to the response stream outside of an HttpHandler is generally not a good idea; it may work in some corner cases, but it's not how things are intended to work.

Have you considered using either a Page base class or a Page Adapter to do this? That way, you would only need one copy of the code, and it could be applied to either all pages or just the ones you select.

Another option would be to use URL rewriting to redirect the incoming request to a page that generates the script output you need.

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