我为HandleErrorAttribute注册GlobalFilters:
public class AppHandleErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
Exception ex = filterContext.Exception;
//TODO
//LogManager.GetLogger("Exception").Error(ex.Message);
if (filterContext.Exception is UserException){
if(!string.isNullOrEmpty(this.View))
{
filterContext.ExceptionHandled = true;
filterContext.Result = ...;//<===this.View(custom Page)
}
else{
filterContext.ExceptionHandled = true;
filterContext.Result = ...;//<==='XYZ' page(another custom page)
}
}
}
}
并在Web.Config Set:
<customErrors mode="On"/>
修改开始
在FilterConfig中我设置:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
//filters.Add(new HandleErrorAttribute());
filters.Add(new AppHandleErrorAttribute() );
}
结束
然后我只想要'Test()'的Action来运行AppHandleErrorAttribute for Once。
public class XXXController:Controller{
public ActionResult Test()
{
throw new UserException("test0x11", "test", null);
return View();
}
[AppHandleError(View="Index")]//<=======here I want the Test2 to Index View, but it will be call AppHandleError twice this time
//it always Redirect to 'XYZ' page
public string Test2()
{
throw new UserException("test0x12", "test", null);
return "haha";
}
public string Index(){...}
}
我怎么能不调用全局HandleError?