Handling Exceptions in ASP.Net MVC 1
Here’s a handy way to handle uncaught exceptions in ASP.Net MVC 1.
Make a base controller with the following code:
public class BaseController : Controller
{
ILog log = LogManager.GetLogger(“BaseController”);
string actionName = “”;
string controllerName = “”;
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
actionName = filterContext.ActionDescriptor.ActionName;
controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
}
protected override void OnException(ExceptionContext filterContext)
{
log.Fatal(“Unhandled Exception”, filterContext.Exception);
//Displays a friendly error, doesn’t require HandleError
filterContext.ExceptionHandled = true;
this.View(“Error”, new HandleErrorInfo(filterContext.Exception, controllerName, actionName)).ExecuteResult(this.ControllerContext);
}
}
Then make your other controllers derive from this one and hey presto, friendly(er) error messages and better exception logging in your site.
Sources:
http://stackoverflow.com/questions/362514/asp-net-mvc-current-action

[...] to VoteHandling Exceptions in ASP.Net MVC 1 (11/19/2009)Thursday, November 19, 2009 from andrewmyhre.wordpress.comHere’s a handy way to handle uncaught [...]
ASP.NET MVC Archived Blog Posts, Page 1
29 Nov 09 at 2:53 pm edit_comment_link(__('Edit', 'sandbox'), ' ', ''); ?>
you probably want to not force inheritance. you could do all that function in an attribute, like these…
public class LogExceptionAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (null != filterContext.Exception)
{
ExceptionPolicy.HandleException(filterContext.Exception, “Exception Policy”);
}
}
}
thomas
18 Dec 09 at 8:26 am edit_comment_link(__('Edit', 'sandbox'), ' ', ''); ?>
Love it, thanks for the tip. I haven’t mucked around with custom action attributes much but I’ll definitely give this a go.
andrewmyhre
18 Dec 09 at 10:20 pm edit_comment_link(__('Edit', 'sandbox'), ' ', ''); ?>