How do I detect if a request is a callback in the Global.asax?

空扰寡人 提交于 2019-12-05 14:06:56

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!

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!