views:

207

answers:

3

I need to find a way to detect if a request is a callback when the Application_BeginRequest method is called.

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
       Dim _isCallBack As Boolean = False
 
       [Code to set _isCallBack is True or False Here]
 
       If Not _isCallBack Then
          ... Some Code
          End If
    End Sub

I need to know what to replace "[Code to set _isCallBack is True or False Here]" with.

+1  A: 

Depends on the context of your question. I see you are talking about ASP.NET in the tags, using VB.NET. You can probably use:

If Not Request.IsPostback Then
  ' Your code here
End If
Marcus Griep
+1  A: 

This may help you: http://msdn.microsoft.com/en-us/magazine/cc163941.aspx
Search for the word __CALLBACKID:

*To determine the callback mode, the ASP.NET runtime looks for a __CALLBACKID entry in the Request collection. If such an entry is found, the runtime concludes that a callback invocation is being made.*

We needed to do this from within an app_code file where access to the Page.xxxx objects was not available. This is the code I ended up using:

If Not IsNothing(HttpContext.Current.Request("__CALLBACKID")) Then
    'The request is a callback
Else
    'The request is not a callback
End If

Maybe not the prettiest solution, but it does the job. We were using Array.IndexOf for a while, but it seems that sometimes that form parameter arrives back as lowercase parameter (not sure why or how), and Array.IndexOf is a case sensitive search.

Be careful looking for these kinds of __XXXX request keys. I remember reading somewhere that it's not a good idea to "shortcut" to these elements since their names could change in some future version of .net. Just keep that in mind!

Dean L
A: 

I needed something similar and, following on Dean L's answer, figured .NET itself must know what to do. Looking in the HttpResponse.Redirect method with Reflector, you see code like this:

Page handler = Context.Handler as Page;
if (handler != null && handler.IsCallback)
{
    //Code...
}

Seems to work fine in Global.asax.

Bitwise