下面这个 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 就可以执行了,为什么?
有什么限制?
谢谢.
试试把 filters.Add(new HandleErrorAttribute()); 放在 filters.Add(new HandleProductErrorAttribute() , 0); 之前