Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This solution combines IResultFilter and IExceptionFilter to catch either thrown HttpException or returned HttpStatusCodeResult from within an action.</p> <pre><code>public class CustomViewForHttpStatusResultFilter: IResultFilter, IExceptionFilter { string viewName; int statusCode; public CustomViewForHttpStatusResultFilter(HttpStatusCodeResult prototype, string viewName) : this(prototype.StatusCode, viewName) { } public CustomViewForHttpStatusResultFilter(int statusCode, string viewName) { this.viewName = viewName; this.statusCode = statusCode; } public void OnResultExecuted(ResultExecutedContext filterContext) { HttpStatusCodeResult httpStatusCodeResult = filterContext.Result as HttpStatusCodeResult; if (httpStatusCodeResult != null &amp;&amp; httpStatusCodeResult.StatusCode == statusCode) { ExecuteCustomViewResult(filterContext.Controller.ControllerContext); } } public void OnResultExecuting(ResultExecutingContext filterContext) { } public void OnException(ExceptionContext filterContext) { HttpException httpException = filterContext.Exception as HttpException; if (httpException != null &amp;&amp; httpException.GetHttpCode() == statusCode) { ExecuteCustomViewResult(filterContext.Controller.ControllerContext); // This causes ELMAH not to log exceptions, so commented out //filterContext.ExceptionHandled = true; } } void ExecuteCustomViewResult(ControllerContext controllerContext) { ViewResult viewResult = new ViewResult(); viewResult.ViewName = viewName; viewResult.ViewData = controllerContext.Controller.ViewData; viewResult.TempData = controllerContext.Controller.TempData; viewResult.ExecuteResult(controllerContext); controllerContext.HttpContext.Response.TrySkipIisCustomErrors = true; } } </code></pre> <p>You can register this filter so, specifying either the http status code of the HttpException or the concrete HttpStatusCodeResult for which you want to display the custom view.</p> <pre><code>GlobalFilters.Filters.Add(new CustomViewForHttpStatusResultFilter(new HttpNotFoundResult(), "Error404")); // alternate syntax GlobalFilters.Filters.Add(new CustomViewForHttpStatusResultFilter(404, "Error404")); </code></pre> <p>It handles exceptions and HttpStatusCodeResult thrown or returned within an action. It won't handle errors that occur before MVC selects a suitable action and controller like this common problems:</p> <ul> <li>Unknown routes</li> <li>Unknown controllers</li> <li>Unknown actions</li> </ul> <p>For handling these types of NotFound errors, combine this solution with <a href="https://stackoverflow.com/questions/619895/how-can-i-properly-handle-404s-in-asp-net-mvc">other solutions to be found in stackoverflow.</a></p>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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