我有一个全局ErrorHanlder(从HandleErrorAttribute派生),可以正常工作并捕获发生的所有错误。但是我有一种情况,家庭 Controller 会抛出异常。我检查了一下,发现从 Controller 抛出的错误未在我的自定义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();
}
}
最佳答案
尽管此时您的代码片段尚不清楚,但看起来_ServiceAsync正在 Controller 的构造函数中初始化。 HandleError过滤器不处理在 Controller 构造期间引发的异常。
有关更多信息,请参见此相关问题:Handling Exceptions that happen in a asp.net MVC Controller Constructor
关于asp.net-mvc-4 - Controller 中的asp.net mvc异常未在自定义ErrorHandler上处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29877339/