C# ASP.NET MVC Controller.OnException 未被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/960280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC Controller.OnException not being called
提问by Stephen franklin
I have a base controller class where I'm overriding to the Controller.OnException handler method in order to provide a generic error handling for certain types of controllers that will inherit from this class (these are API controllers that will return JSON results). The OnException method never gets called when an exception gets raised by the controller. Does anyone see what I am doing wrong, or is there a better way to handle this situation?
我有一个基本控制器类,我在其中覆盖了 Controller.OnException 处理程序方法,以便为将从此类继承的某些类型的控制器(这些是将返回 JSON 结果的 API 控制器)提供通用错误处理。当控制器引发异常时,永远不会调用 OnException 方法。有没有人看到我做错了什么,或者有没有更好的方法来处理这种情况?
Using MVC 1.0
使用 MVC 1.0
Base Class:
基类:
public class APIController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
//This is never called
filterContext.Result =
new JsonResult();
base.OnException(filterContext);
}
}
Inheriting Class:
继承类:
public class MyController : APIController
{
public AjaxResult ForcedException()
{
throw new SystemException();
}
}
回答by Razvan Dimescu
Apply the [HandleError]
attribute to the class.
将[HandleError]
属性应用 到类。
回答by eu-ge-ne
If I understand your question correctly - your Exception must be marked as "handled" in your OnException. Try this:
如果我正确理解您的问题 - 您的异常必须在 OnException 中标记为“已处理”。尝试这个:
filterContext.ExceptionHandled = true;
回答by phokus
The MSDN documentation (http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onexception.aspx) states that the OnException method is "called when an unhandled exception occurs in the action."
MSDN 文档 ( http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.oneexception.aspx) 指出 OnException 方法“在操作中发生未处理的异常时调用”。
So, it sounds like it might only fire when an unhandled exception happens in an action method.
因此,听起来它可能仅在操作方法中发生未处理的异常时才触发。
I created an new controller with one Index action. And the OnException method does in fact execute. I also tried throwing a SystemException() and the OnException method fired as well.
我创建了一个带有一个索引操作的新控制器。并且 OnException 方法确实会执行。我还尝试抛出 SystemException() 并且也触发了 OnException 方法。
public ActionResult Index ( )
{
throw new NotImplementedException ( );
}
回答by Seth Petry-Johnson
Are you calling the controller action from a unit test, or via ASP.NET? If you're calling the method directly in a test, say through NUnit, then OnException won't fire: the test framework will handle the exception and turn it into a failed test.
您是通过单元测试还是通过 ASP.NET 调用控制器操作?如果您在测试中直接调用该方法,例如通过 NUnit,则 OnException 不会触发:测试框架将处理异常并将其转换为失败的测试。
回答by Benjamin Gale
I was having exactly the same problem in my ASP.NET MVC 3 project.
我在我的 ASP.NET MVC 3 项目中遇到了完全相同的问题。
It turns out that this is a feature of Visual Studio and the development server. When in debug mode you will be returned to Visual Studio and given the default exception dialog box.
事实证明,这是 Visual Studio 和开发服务器的一个特性。在调试模式下,您将返回到 Visual Studio 并显示默认异常对话框。
One way to stop this from happening is to change the compilation mode in your Web.config
from this:
阻止这种情况发生的一种方法是从以下位置更改编译模式Web.config
:
<compilation debug="true" targetFramework="4.0">
To this:
对此:
<compilation debug="false" targetFramework="4.0">
You can also leave debug mode set to true but test your application is working by choosing the Start without debugging option
from the visual studio menu (invoked by pressing Ctrl+F5
.
您还可以将调试模式设置为 true,但通过Start without debugging option
从 Visual Studio 菜单中选择 来测试您的应用程序是否正常工作(通过按Ctrl+F5
.
回答by seangwright
The same rules apply for Controller.OnException
as for HandleErrorAttribute
相同的规则适用Controller.OnException
于HandleErrorAttribute
The required config for HandleErrorAttribute
is noted here https://stackoverflow.com/a/528550/939634
HandleErrorAttribute
此处注明了所需的配置https://stackoverflow.com/a/528550/939634
If you have customError
in your web.config
set to mode="RemoteOnly"
and you are debugging locally or you have mode="Off"
the exceptions will not be handled and you will see the ASP.Net yellow screen with a stack trace.
如果customError
您web.config
设置为mode="RemoteOnly"
并且您正在本地调试或者您有mode="Off"
异常将不会被处理,您将看到带有堆栈跟踪的 ASP.Net 黄色屏幕。
If you set mode="On"
or you are viewing the site remotely and have mode="RemoteOnly"
your OnException
method will execute properly.
如果您设置mode="On"
或正在远程查看站点并且mode="RemoteOnly"
您的OnException
方法将正确执行。