控制器中的asp.net mvc异常不在自定义ErroHandler处理

时间:2015-04-26 12:01:07

标签: asp.net-mvc-4 error-handling

我有一个全局的ErrorHanlder(从HandleErrorAttribute派生),可以正常工作并捕获所有发生的错误。但我有一种情况,家庭控制器可以抛出异常。我检查并看到从控制器抛出的错误没有在我的自定义ErrorHandler中处理,asp.net给出了:

Exception of type 'Exception' was thrown.

An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

请你帮帮我吗?

代码:

    public HomeController()
            {

                    _ServiceAsync = new ServiceAsyncProvider();

            }

class ServiceAsyncProvider
{
   public ServiceAsyncProvider()
     {
            throw new Exception(); 
     }
}

2 个答案:

答案 0 :(得分:1)

虽然您的代码片段在这一点上有点不清楚,但看起来您的_ServiceAsync正在控制器的构造函数中初始化。 HandleError过滤器不处理控制器构造期间抛出的异常。

有关详细信息,请参阅此相关问题:Handling Exceptions that happen in a asp.net MVC Controller Constructor

答案 1 :(得分:1)

您在构造函数中抛出异常。过滤器不处理这些异常。

相关问题