Problem with general error handling of the base controller in ASP.NET MVC

There is a trick in my base controller that handles errors that I will not catch. This is something like this:

protected override void OnException(ExceptionContext filterContext) { // Bail if we can't do anything if (filterContext == null) return; // log var ex = filterContext.Exception ?? new Exception("No further information exists."); LogException(ex); filterContext.ExceptionHandled = true; var data = new ErrorPresentation { ErrorMessage = HttpUtility.HtmlEncode(ex.Message), TheException = ex }; filterContext.Result = View("Error", data); base.OnException(filterContext); } 

Now it works fine if there is an error in the controller . The problem is that if an error occurs while rendering the aspx page (say, in the HtmlHelper method), a complete view of the error is displayed, in place on the page where the error occurred . This means that the entire error page (MasterPage and all) receives a visualization on the error page. Not quite expected behavior.

I tried to change the view to RedirectToAction, but this will not work, since the route does not exist for each controller (Main / Error, Configuration / Error, etc.).

How can I make this work for both errors in the controller and errors on the page?

+7
asp.net-mvc error-handling
source share
2 answers

You will need a way to track the state of the page so that you can distinguish whether you are performing an action - in this case you want to replace the result - or execute the result - in this case you want to discard the contents of the response and execute a new ViewResult for the error. Storing a flag in the base controller will be one way to handle it. You can also specify based on the type of exception. I'm not sure how reliable it will be.

Once you find out the state you can make:

  var view = View("Error", data ); if (executingResult) { filterContext.HttpContext.Response.ClearContent(); view.ExecuteResult(); } else { filterContext.Result = view; base.OnException( filterContext ); } 

Comment: I usually do not advocate this. I think that the action of your controller should guarantee View that all the data it needs is there or your View should be protected to ensure that exceptions (especially NullReferenceExceptions) do not occur.

+1
source share

This is an old question, but for those who are desperately looking, this code works for me, for exceptions anywhere - in the controller, action or action, that is, in the view or any helpers that the call causes. (I got it from Steve Sanderson's book to give because of where it belongs):

 protected override void OnException(ExceptionContext filterContext) { if (filterContext.ExceptionHandled) return; //Let the request know what went wrong filterContext.Controller.TempData["Exception"] = filterContext.Exception; //redirect to error handler filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary( new { controller = "Exception", action = "HandleError" })); // Stop any other exception handlers from running filterContext.ExceptionHandled = true; // CLear out anything already in the response filterContext.HttpContext.Response.Clear(); } 
+12
source share

All Articles