Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 为什么我的异常过滤器无法捕获?_Asp.net Mvc - Fatal编程技术网

Asp.net mvc 为什么我的异常过滤器无法捕获?

Asp.net mvc 为什么我的异常过滤器无法捕获?,asp.net-mvc,Asp.net Mvc,这是在ASP.NETMVC4.5上实现的。我正在尝试让一个基本的异常过滤器工作。我一直在Filters.dll中遇到“System.ArgumentOutOfRangeException”类型的异常,但未在用户代码中处理,但我认为已设置为处理该异常: 家庭/范围测试/50 RangeExceptionAttribute.cs HomeController.cs 有人建议我注册过滤器,但在我正在关注的书中,他们从来不需要注册任何东西。但我仍然尝试通过以下方式进行注册: // FilterConfi

这是在ASP.NETMVC4.5上实现的。我正在尝试让一个基本的异常过滤器工作。我一直在Filters.dll中遇到“System.ArgumentOutOfRangeException”类型的异常,但未在用户代码中处理,但我认为已设置为处理该异常:

家庭/范围测试/50

RangeExceptionAttribute.cs

HomeController.cs

有人建议我注册过滤器,但在我正在关注的书中,他们从来不需要注册任何东西。但我仍然尝试通过以下方式进行注册:

// 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);
    }
}

答案是,它在异常处理之前就被破坏了。这个异常本来可以得到很好的处理,但是因为我看到了未处理的异常对话框,所以我一直停止程序,而不是按continue,此时我的异常过滤器会处理错误

您可以通过取消选中此框来解决问题:


该异常可能首先被包装在另一个异常中,例如HttpException。您是否查看过过滤器拾取的异常的InnerException?我认为它是一个没有内部异常的ArgumentOutOfRange异常。不过我可能看错了。这是一个屏幕截图,我的意思是,当MVC看到异常时,它可能不再是ArgumentOutOfRange异常。如果你在调试器的过滤器的第一行设置断点,它会被命中吗?啊,对不起,不。我在过滤器的OneException方法的第一行上放置了一个断点,观察到它根本不会进入过滤器。您的IDE的异常处理设置是否使未处理的异常总是导致调试器调用?如果在“未处理的异常”对话框中按“继续”,它是否会流向过滤器?
    [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);
    }
}