I have the following setup:
- Web.config has customErrors mode=”Off”
- Global.Application_Error() event calls a custom function called UtilitiesWeb.ProcessError()
- UtilitiesWeb.ProcessError() logs the error, then redirects the user with: HttpContext.Current.Response.Redirect(defaultErrorPage, false)
This works well as a good global error handler, except when dealing with AJAX. AJAX displays the error with in JavaScript, and ignores the redirect.
To get the page to redirect on an AJAX error I had to do the following:
- Update asp:ScriptManager to: <asp:ScriptManager ID=”scriptmanager1″ runat=”server”
OnAsyncPostBackError=”ScriptManager1_AsyncPostBackError”> - Add Server side ScriptManager1_AsyncPostBackError method that calls UtilitiesWeb.ProcessError()
- Add the following JavaScript to redirect because AJAX ignores the server side redirect:
<script type="text/javascript"> if (typeof (Sys) != 'undefined') { Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest); function endRequest(sender, e) { if (e.get_error()) { window.location = "ErrorPage.aspx" } } } </script>
But all this seems like a workaround. Is there way to have a server side redirect that works with AJAX errors?
For completeness, here is the code for UtilitiesWeb.ProcessError():
public static void ProcessError()
{
string defaultErrorPage = ConfigurationManager.AppSettings["DefaultErrorPage"];
string displayError = ConfigurationManager.AppSettings["DisplayError"];
Exception ex;
if (!string.IsNullOrWhiteSpace(displayError))
displayError = displayError.ToLower();
ex = HttpContext.Current.Server.GetLastError();
if (ex != null)
Log.LogException(DateTime.Now, null, ex);
// Redirect to Default Error page on error
if (displayError == "basic")
{
HttpContext.Current.Server.ClearError(); // needed for redirect to work
if (!string.IsNullOrWhiteSpace(defaultErrorPage))
HttpContext.Current.Response.Redirect(defaultErrorPage, false); // doesn't work with AJAX calls
}
}
When you’re making an AJAX request, you just get a response containing some kind of data, which is interpreted by scripts on the client side.
So, in case on error, the only thing that the server can do is send the error in the response to the ajax request.
At this moment, the client-side script receives the error information and you can use it the way you like. You’re doing it the right way.
However, if you need to take some extra actions, like logging the error, you have two options:
1) send the error back to the server from the javascript: for example you could use aregular form with an input of type hidden, set the input value, and post the form using form.submti() to your error page. Your page can recover the contents of the hidden field using the Page’s Request object and process it.
2) catch the error on server side, within try/catch, or page_error or application_error events, log the exception, or whatever you want to do with it, and throw a new exception that will arrive at the client side, hiding the original exception info.
The second option is cleaner and safer, as you don’t send error information to the browser.
Think that the server can’t redirect the browser in any way because when the server is sending a reponse to an AJAX request the server isn’t rendering a new page for the browser,and the browser is not receiving a new whole page. The server is simply sending some information to be processed by client-side scripts. So your script is responsible for reloading a new page or whatever you need to do.