默认错误处理跳转是由HandleErrorAttribute处理的,
public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); } }
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
在Global.asax注销上面一段,就可使用web.config中的配置
<system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> <customErrors mode="On" defaultRedirect="~/error.html" /> </system.web>
还可以自己重写HandleErrorAttribute
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new ExceptionFilterAttribute()); //自己定义的过滤器 } /// <summary> /// 2.自己定义的过滤器 处理异常跳转错误页 /// 捕获异常 跳转到错误页 /// </summary> public class ExceptionFilterAttribute : HandleErrorAttribute { public override void OnException(ExceptionContext filterContext) { base.OnException(filterContext); string excepitonInfo = filterContext.Exception.ToString(); LogHelper.OutputLog(excepitonInfo); filterContext.HttpContext.Response.Redirect("/WebPages/Error.htm"); } }