Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To preserve the original URL you could go the route described <a href="http://www.wduffy.co.uk/blog/using-application_error-in-asp-net-mvcs-global-asax-to-handle-errors/" rel="nofollow">here</a> and handle errors explicitely inside global.asax instead of using web.config customerrors or httperrors sections (which can still be configured for fallback) but change the IHttpHandler.ProcessRequest part on linked websites example with HttpContext.Server.TransferRequest(path, true). I've implemented the Application_Error event like this in one of my projects:</p> <pre><code>protected void Application_Error() { Exception error = Server.GetLastError(); var code = (error is HttpException) ? (error as HttpException).GetHttpCode() : 500; if (!Context.IsDebuggingEnabled &amp;&amp; code != 404) { // persist error to error log db //Log.Logger.Error("unhandled exception: ", exception); } if (WebConfigurationManager.AppSettings["EnableCustomExceptionPage"].Equals("false")) { return; } Response.Clear(); Server.ClearError(); string path = this.Request.Path; string action; switch (code) { case 401: action = "Unauthorized"; break; case 403: action = "Forbidden"; break; case 404: action = "NotFound"; break; default: action = "Generic"; break; } string newPath = string.Format("~/Error/{0}?source={1}&amp;message={2}", action, path, HttpUtility.UrlEncode(error.Message)); Context.Server.TransferRequest(newPath, true); } </code></pre> <p>Internally it transfers the request to a new path handled i.e. by an ErrorController like in the described example above which actions could look like this:</p> <pre><code>public ViewResult Generic(string source, string message) { Response.TrySkipIisCustomErrors = true; Response.StatusCode = 500; ViewBag.Source = source; ViewBag.Message = HttpUtility.UrlDecode(message); return View(); } public ViewResult NotFound(string source, string message) { Response.TrySkipIisCustomErrors = true; Response.StatusCode = 404; ViewBag.Source = source; ViewBag.Message = HttpUtility.UrlDecode(message); return View(); } ... </code></pre> <p>The <code>TrySkipIisCustomErrors = true</code> prevents IIS from redirecting to the default custom error pages. You can handle thrown Exceptions (like special Exceptions thrown in business/service layer) other than HttpException differently inside Application_Error override method.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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