If your controller uses the HandleError
-Attribute it handles exceptions thrown by action methods. I wanted to catch these exceptions and redirect to a custom error page. A simple RedirectToAction
inside the OnException
method does not work. But the ExceptionContext
is all we need.
#region Error handling protected override void OnException(ExceptionContext filterContext) { // Make use of the exception later this.Session["ErrorException"] = filterContext.Exception; // Mark exception as handled filterContext.ExceptionHandled = true; // ... logging, etc // Redirect filterContext.Result = this.RedirectToAction("Error", "Home"); base.OnException(filterContext); } #endregion |
It stored the exception thrown inside the action onto the session. So this information is available inside my "error-action".
My custom "error-action" even allows me to distinguish between normal requests and Ajax-requests (where I use a partial-view instead of a normal view) to display a nicer error message to the end-user.