I’m trying to get my head around the Error Handling in MVC. What I’m looking for is a centralized way to catch errors, log them, if possible resolve them, if nessecary take other actions and finally show the correct view to the user.
I think I can use the [HandleError] filter for this, but I don’t see any way to route it to a Controller/Action. The only option I see is pointing it directly to a view.
HandleErrorAttribute that comes with MVC is a pretty basic IExceptionFilter.
You have a few options to achieve what i think u want.
You can either use [HandleError (Type=typeof(MyException),View=’ErrorView’)] on actions/controllers or implement your own
HandleErrorAttribute isnt very complex. I think MS recommends you copy this code and modify to suit your requirements.
The OnException override gives you access to all that info you may need – controller, action, route data, etc – through ExceptionContext.
Remember to set the ExceptionHandled. Then you can set the filterContext.Result to a new RedirectToAction instance which redirect to your ErrorController and action – obviously you can expose the specific controller and action with properties.