views:

589

answers:

3

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.

A: 

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.

John Downey
+1  A: 

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!

A: 

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
Adam