Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to issue server side Redirect with AJAX error
    primarykey
    data
    text
    <p>I have the following setup:</p> <ol> <li>Web.config has customErrors mode="Off"</li> <li>Global.Application_Error() event calls a custom function called UtilitiesWeb.ProcessError()</li> <li>UtilitiesWeb.ProcessError() logs the error, then redirects the user with: HttpContext.Current.Response.Redirect(defaultErrorPage, false)</li> </ol> <p>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. </p> <p>To get the page to redirect on an AJAX error I had to do the following:</p> <ol> <li>Update asp:ScriptManager to: &lt;asp:ScriptManager ID="scriptmanager1" runat="server" <strong>OnAsyncPostBackError="ScriptManager1_AsyncPostBackError"</strong>&gt;</li> <li>Add Server side ScriptManager1_AsyncPostBackError method that calls UtilitiesWeb.ProcessError()</li> <li>Add the following JavaScript to redirect because AJAX ignores the server side redirect:</li> </ol> <blockquote> <pre><code>&lt;script type="text/javascript"&gt; if (typeof (Sys) != 'undefined') { Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest); function endRequest(sender, e) { if (e.get_error()) { window.location = "ErrorPage.aspx" } } } &lt;/script&gt; </code></pre> </blockquote> <p>But all this seems like a workaround. Is there way to have a server side redirect that works with AJAX errors? </p> <p>For completeness, here is the code for UtilitiesWeb.ProcessError():</p> <pre><code>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 } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload