在MVC4中多次调用global HandleErrorAttribute和自定义Action HandleErrorAttribute

时间:2013-11-15 05:21:46

标签: c# asp.net-mvc filter

我为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?

0 个答案:

没有答案
相关问题