这是在ASP.NET MVC 4.5上。我试图让一个基本的异常过滤器工作。我不断获得An exception of type 'System.ArgumentOutOfRangeException' occurred in Filters.dll but was not handled in user code
,但我认为设置处理该异常:
Home/RangeTest/50
RangeExceptionAttribute.cs
public class RangeExceptionAttribute : FilterAttribute, IExceptionFilter {
public void OnException(ExceptionContext filterContext)
{
if (!filterContext.ExceptionHandled && filterContext.Exception is ArgumentOutOfRangeException)
{
filterContext.Result = new RedirectResult("~/Content/RangeErrorPage.html");
filterContext.ExceptionHandled = true;
}
}
}
HomeController.cs
[RangeException]
public string RangeTest(int id)
{
if (id > 100)
return String.Format("The id value is: {0}", id);
else
throw new ArgumentOutOfRangeException("id", id, "");
}
有人建议我注册过滤器,但在我关注的书中,他们永远不必注册任何东西。但我仍然试图通过这样做来注册:
// FilterConfig.cs
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new RangeExceptionAttribute());
}
// Global.asax.cs
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalFilters.Filters.Add(new Filters.Infrastructure.RangeExceptionAttribute());
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
答案 0 :(得分:0)
答案是在处理异常之前它已经崩溃了。该异常实际上已经处理好但是因为我看到了未处理的异常对话框,我一直停止程序而不是按下继续(此时我的异常过滤器确实处理了错误)。
您可以通过取消选中此框来解决问题: