public class EwHandleErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
if (ICTConfiguration.Debug)
{
base.OnException(filterContext);
return;
} if (filterContext.ExceptionHandled)
{
return;
}
if (filterContext.HttpContext.Response.IsRequestBeingRedirected)
{
return;
}
var httpCode = new HttpException(null, filterContext.Exception).GetHttpCode();
if (!ExceptionType.IsInstanceOfType(filterContext.Exception))
{
return;
}
if (new HttpException(null, filterContext.Exception).GetHttpCode() != )
{
return;
}
ExceptionHelper.LogException(filterContext.Exception, HttpContext.Current);
bool isAjaxCall = string.Equals("XMLHttpRequest", filterContext.HttpContext.Request.Headers["x-requested-with"],
StringComparison.OrdinalIgnoreCase);
if (isAjaxCall)
{
string message = filterContext.Exception.Message;
if (filterContext.Exception is HttpRequestValidationException)
{
message = "包含非法字符";
} filterContext.Result = new JsonResult()
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = new
{
succeed = false,
ret = httpCode,
msg = message
}
};
}
else
{
var controllerName = (string)filterContext.RouteData.Values["controller"];
var actionName = (string)filterContext.RouteData.Values["action"];
var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
filterContext.Result = new ViewResult()
{
ViewName = View,
MasterName = Master,
ViewData = new ViewDataDictionary(model),
TempData = filterContext.Controller.TempData
};
filterContext.HttpContext.Response.Redirect("/500.html");
}
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
filterContext.HttpContext.Server.ClearError();
}
}
global注册过滤器
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new JoinEnterpriseAttribute(), );
filters.Add(new EwHandleErrorAttribute(), );
}