0

我为 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 集中:

<customErrors mode="On"/>

编辑开始

在 FilterConfig 我设置:

 public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        //filters.Add(new HandleErrorAttribute());
        filters.Add(new AppHandleErrorAttribute() );
    }

结尾

然后我只想让“Test()”的动作运行 AppHandleErrorAttribute 一次。

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(){...}
}

我怎样才能不调用 globle HandleError?

4

0 回答 0