Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Look at the HandleErrorAttribute. Subclass from it or add your own implementation which will handle all the status codes you're interested in. You can make it to return a separate error view for each error type.</p> <p>Here is an idea of how to create your handle error exception filter. I've thrown out most of the stuff to only focus on our essentials. Absolutely have a look at the original implementation to add arguments checks and other important things.</p> <pre><code>public class HandleManyErrorsAttribute : FilterAttribute, IExceptionFilter { public virtual void OnException(ExceptionContext filterContext) { if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled) return; Exception exception = filterContext.Exception; string viewName = string.Empty; object viewModel = null; int httpCode = new HttpException(null, exception).GetHttpCode(); if (httpCode == 500) { viewName = "Error500View"; viewModel = new Error500Model(); } else if (httpCode == 404) { viewName = "Error404View"; viewModel = new Error404Model(); } else if (httpCode == 401) { viewName = "Error401View"; viewModel = new Error401Model(); } string controllerName = (string)filterContext.RouteData.Values["controller"]; string actionName = (string)filterContext.RouteData.Values["action"]; filterContext.Result = new ViewResult { ViewName = viewName, MasterName = Master, ViewData = viewModel, TempData = filterContext.Controller.TempData }; filterContext.ExceptionHandled = true; filterContext.HttpContext.Response.Clear(); filterContext.HttpContext.Response.StatusCode = httpCode; filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; } } </code></pre> <p>Then you "decorate" your controller actions with this attribute:</p> <pre><code>[HandleManyErrors] public ActionResult DoSomethingBuggy () { // ... } </code></pre>
 

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