/// <summary> /// Handles the standard filter method. /// </summary> /// <param name="filterContext"></param> public void OnException(ExceptionContext filterContext) { JsonErrorResult errorResult; if ( // has the exception already been handled? filterContext.ExceptionHandled // there gots to be an exception! || (filterContext.Exception == null) ) { // don't process it, just return return; } // if an exception type has been defined and the exception is NOT that type if (this.ExceptionType != null && this.ExceptionType != filterContext.Exception.GetType()) { return; } // if we only care about handling on ajax requests and this is NOT one... if (AjaxOnlyRequest && !filterContext.RequestContext.HttpContext.Request.IsAjaxRequest()) { // ... then don't do anything. return; } // if 1 or more accept types have been specified, if (AcceptTypes != null && AcceptTypes.Length > 0) { // see that at least 1 of the types exists in both lists. if (AcceptTypes.Join(filterContext.HttpContext.Request.AcceptTypes, t => t, t => t, (t, r) => t).Count() == 0) { return; } } errorResult = JsonErrorResult.Create(IncludeException ? filterContext.Exception : null, this.Message); // set the call result to the JSON error result package filterContext.Result = new JsonResult() { Data = errorResult, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; // mark the exception as handled to stop further error filter handling filterContext.HttpContext.Response.StatusCode = HttpStatusCode; filterContext.ExceptionHandled = true; NotifyExceptionHandled(filterContext); }