Wednesday, November 11, 2009

How to filter Exceptions (Such as null parameter) in MVC?

Override your OnException in your controller:
protected override void OnException(ExceptionContext filterContext)
{
if (IsNullParamter(filterContext.Exception))
filterContext.HttpContext.Response.Redirect("~/Home/Index");
base.OnException(filterContext);
}

private bool IsNullParamter(System.Exception ex)
{
string error = ex.Message;
if (error.Contains("System.ArgumentException: The parameters dictionary contains a null entry for parameter")) return true;
if (error.Contains("The parameters dictionary contains a null entry for parameter")) return true;
return false;
}

No comments:

Post a Comment