首页新闻找找看学习计划

自定义 ExceptionFilter 没有执行

0
悬赏园豆:30 [已解决问题] 解决于 2012-06-07 14:27

下面这个 ExceptionFilter 在我本地用 IISExpress 执行的非常完美,但是我把它发布到测试服务器上, 却没有执行.不懂为什么.

 

public class HandleProductErrorAttribute : FilterAttribute , IExceptionFilter {

    public void OnException(ExceptionContext filterContext) {

        if(!filterContext.ExceptionHandled && filterContext.Exception is ProductInvalidException) {
            var ex = filterContext.Exception as ProductInvalidException;
            var view = "";
            switch(ex.Tag) { 
                case InvalidProductTag.PriceChanged:
                    view = "PriceChange";
                    break;
                case InvalidProductTag.HotelInvalid:
                case InvalidProductTag.FlightInvalid:
                    view = "Full";
                    break;
                case InvalidProductTag.HotelNotRealTime:
                    view = "NotRealTime";
                    break;
                case InvalidProductTag.Error:
                    view = "Exception";
                    break;
            }

            if(!string.IsNullOrEmpty(view)) {
                string controllerName = (string)filterContext.RouteData.Values["controller"];
                string actionName = (string)filterContext.RouteData.Values["action"];
                HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception , controllerName , actionName);
                ViewResult result = new ViewResult {
                    ViewName = view ,
                    ViewData = new ViewDataDictionary<HandleErrorInfo>(model) ,
                    TempData = filterContext.Controller.TempData
                };
                filterContext.Result = result;
                filterContext.ExceptionHandled = true;
                filterContext.HttpContext.Response.Clear();
                filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
            }
        }

    }

}

Global

    public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
        filters.Add(new HandleProductErrorAttribute() , 0);
        filters.Add(new HandleErrorAttribute() , 1);

        //filters.Add(new HandleProductErrorAttribute());
        //filters.Add(new HandleErrorAttribute());
        //不指定执行顺序也一样, 不会执行.
    }

当我把默认的 HandleErrorAttribute 那句删除, 我自定义的 ExceptionFilter 就可以执行了,为什么?

有什么限制?

谢谢.

xling的主页 xling | 初学一级 | 园豆:6
提问于:2012-06-06 14:20
< >
分享
最佳答案
0

试试把 filters.Add(new HandleErrorAttribute()); 放在 filters.Add(new HandleProductErrorAttribute() , 0); 之前

收获园豆:30
dudu | 高人七级 |园豆:39136 | 2012-06-07 08:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册