检查 Application_BeginRequest 上是否有 IsCallback

发布于 2024-07-26 15:29:00 字数 604 浏览 8 评论 0原文

我有一个 Web 应用程序(.NET 3.5),它在 Global.asax 中包含以下代码:

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    LinkLoader()
    PathRewriter()
    PathAppender()
End Sub

我希望调用其中的所有函数,除非它是 AJAX 回调。 因此,理想情况下我会将其更改为:

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    If not Page.IsCallback then
        LinkLoader()
        PathRewriter()
        PathAppender()
    End If
End Sub

但此处无法访问页面对象。 所以,基本上我的问题是:

如何检查请求是否是 Application_BeginRequest 内的 AJAX 回调?

非常感谢您的任何反馈。

I have a web application (.NET 3.5) that has this code in Global.asax:

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    LinkLoader()
    PathRewriter()
    PathAppender()
End Sub

I want all those functions inside to get called except for when it's an AJAX call back. So, ideally I would have it changed to:

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    If not Page.IsCallback then
        LinkLoader()
        PathRewriter()
        PathAppender()
    End If
End Sub

But there is not access to the page object here. So, basically my question is:

How do I check if the request is an AJAX call back inside Application_BeginRequest?

Thank you very much for any feedback.

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

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

发布评论

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

评论(3

沉默的熊 2024-08-02 15:29:00

约翰,

谢谢你为我指明了正确的方向。 解决方案实际上是检查 Request.Form("__ASYNCPOST")。 如果它是回调,则将其设置为“true”。

非常感谢你的帮助!

John,

Thanks for pointing me in the right direction. The solution is actually to check for Request.Form("__ASYNCPOST"). It is set to "true" if it is a CallBack.

Thanks so much for the help!

月下凄凉 2024-08-02 15:29:00

您应该有权访问 HttpContext.Current.Handler 对象,您可以将其转换为 Page 对象并获取 Page.IsPostBack 或 Page.IsCallBack。 尽管为了安全地执行此操作,您需要首先测试它是一个 Page 对象并且不为 null:

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

You should have access to the HttpContext.Current.Handler object which you can cast to a Page object and get Page.IsPostBack or Page.IsCallBack. Although in order to do this safely you need to first test that it is a Page object and not null:

With HttpContext.Current
   If TypeOf .Handler Is Page Then
      Dim page As Page = CType(.Handler, Page)
      If page IsNot Nothing AndAlso (page.IsCallBack OrElse page.IsPostBack) Then
         'Do something
      End If
   End If
End With
我不吻晚风 2024-08-02 15:29:00

根据我的理解,IsCallback 所做的只是检查表单是否有一个名为 __CALLBACKARGUMENT 的 post 变量。 您可以自己在 Context.Request.Form 中检查表单,这应该会告诉您与 IsCallback 相同的信息。

From my understanding all IsCallback does is check if the form has a post variable named __CALLBACKARGUMENT. You could check the form yourself in Context.Request.Form and that should tell you the same thing as IsCallback.

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