How to determine if a request is a callback in Global.asax?

I need to find a way to determine if a request is a callback when calling the Application_BeginRequest method.

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)<br /> 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 install _isCallBack True or False here]" with.

+4
source share
3 answers

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 the __CALLBACKID entry in the request collection. If such an entry is found, the runtime concludes that a callback is being performed.

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

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

This may not be the most pleasant solution, but it does the job. We used Array.IndexOf for a while, but it seems that sometimes this form parameter is returned as lower case (not sure why and how), and Array.IndexOf is case-sensitive.

Be careful when searching for these __XXXX request keys. I remember reading somewhere that it is not recommended to β€œshorten” these elements, since their names may change in some future version of .net. Just keep that in mind!

+3
source

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

  If Not Request.IsPostback Then
   'Your code here
 End if
+1
source

I needed something similar, and, following Dean L's answer, I thought that .NET itself should know what to do. Looking at the HttpResponse.Redirect method with Reflector, you see the following code:

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

Seems to work great at Global.asax.

+1
source

All Articles