How to determine if the current request is an asynchronous postback in an ASP.NET Application_Error event

Is it possible to determine if the current request is an asynchronous postback (partial page refresh) from the Application_Error event?

What is the best way to handle application errors when using asynchronous postbacks?

In Application_Error, we are redirected to different error pages, but this does not work properly when an error occurs during asynchronous postback. We noticed that this is true even if AllowCustomErrorsRedirect = false, and we have an OnAsyncPostBackError handler to set AsyncPostBackErrorMessage. During asynchronous postbacks, our AsyncPostBackErrorMessage is overwritten, and the client receives instead a general webpage error.

+5
source share
3 answers

In the method Application_Error, you no longer have direct access to the element <asp:ScriptManager>on the page. So it's too late to handle its AsyncPostBackError event.

If you want to prevent redirection, you should check the request to make sure that it is actually an asynchronous request. <asp:UpdatePanel>Invokes a message with the following HTTP header:

X-MicrosoftAjax:Delta=true

(also see: ScriptManager allows AJAX in your web applications )

Checking this header will look something like this:

HttpRequest request = HttpContext.Current.Request;
string header = request.Headers["X-MicrosoftAjax"];
if(header != null && header == "Delta=true") 
{
  // This is an async postback
}
else
{
  // Regular request
}

As for the appropriate way to handle the exception, this is another imho question.

+5
source

. Server.ClearError() ScriptManager AsyncPostBackError. Global.asax Application_Error.

+1

Application_Error ScriptManager, , . HttpContext.Current.Handler , ScriptManager, , .

, ScriptManager :

ScriptManager.GetCurrent(CType(HttpContext.Current.Handler, Page)).IsInAsyncPostBack

, , , ScriptManager, , Global.asax, :

Private Function GetCurrentScriptManager() As ScriptManager
    'Attempts to get the script manager for the current page, if there is one

    'Return nothing if the current request is not for a page
    If Not TypeOf HttpContext.Current.Handler Is Page Then Return Nothing

    'Get page
    Dim p As Page = CType(HttpContext.Current.Handler, Page)

    'Get ScriptManager (if there is one)
    Dim sm As ScriptManager = ScriptManager.GetCurrent(p)

    'Return the script manager (or nothing)
    Return sm
End Function

Private Function IsInAsyncPostback() As Boolean
    'Returns true if we are currently in an async postback to a page

    'Get current ScriptManager, if there is one
    Dim sm As ScriptManager = GetCurrentScriptManager()

    'Return false if no ScriptManager
    If sm Is Nothing Then Return False

    'Otherwise, use value from ScriptManager
    Return sm.IsInAsyncPostBack
End Function

IsInAsyncPostback() Application_Error, , .

ASP.NET , / , , , . , .

: ScriptManager , - AsyncPostBackErrorMessage Application_Error . . , ScriptManager OnAsyncPostBackError .

0
source

All Articles