ASP.NET MVC Controller.OnException 未被调用
我有一个基本控制器类,我在其中重写 Controller.OnException 处理程序方法,以便为将从此类继承的某些类型的控制器(这些是将返回 JSON 结果的 API 控制器)提供通用错误处理。 当控制器引发异常时,永远不会调用 OnException 方法。 有谁看到我做错了什么,或者有更好的方法来处理这种情况吗?
使用 MVC 1.0
基类:
public class APIController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
//This is never called
filterContext.Result =
new JsonResult();
base.OnException(filterContext);
}
}
继承类:
public class MyController : APIController
{
public AjaxResult ForcedException()
{
throw new SystemException();
}
}
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?
Using 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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果我正确理解你的问题 - 你的异常必须在 OnException 中标记为“已处理”。 尝试这个:
If I understand your question correctly - your Exception must be marked as "handled" in your OnException. Try this:
MSDN 文档 (http://msdn. microsoft.com/en-us/library/system.web.mvc.controller.oneException.aspx) 指出 OnException 方法“在操作中发生未处理的异常时调用”。
因此,听起来它可能只会在操作方法中发生未处理的异常时触发。
我创建了一个带有一个 Index 操作的新控制器。 OnException 方法实际上会执行。 我还尝试抛出 SystemException() 并触发 OnException 方法。
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."
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.
我在 ASP.NET MVC 3 项目中遇到了完全相同的问题。
原来这是Visual Studio和开发服务器的一个功能。 当处于调试模式时,您将返回到 Visual Studio 并给出默认的异常对话框。
阻止这种情况发生的一种方法是更改
Web.config
中的编译模式:改为:
您还可以将调试模式设置为 true,但通过选择 < code>从 Visual Studio 菜单中启动而不使用调试选项(通过按
Ctrl+F5
调用。I was having exactly the same problem in my ASP.NET MVC 3 project.
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.
One way to stop this from happening is to change the compilation mode in your
Web.config
from this:To this:
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 pressingCtrl+F5
.您是从单元测试还是通过 ASP.NET 调用控制器操作? 如果您直接在测试中调用该方法(例如通过 NUnit),则 OnException 将不会触发:测试框架将处理异常并将其转变为失败的测试。
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.
与
HandleErrorAttribute
相同的规则适用于Controller.OnException
此处注明了
HandleErrorAttribute
所需的配置 https://stackoverflow.com/a/528550/939634如果您的
web.config
中有customError
设置为mode="RemoteOnly"
并且您正在本地调试或者您有mode="Off"
异常将不会被处理,您将看到 ASP.Net 黄屏带有堆栈跟踪。如果您设置了
mode="On"
或者您正在远程查看站点并设置了mode="RemoteOnly"
,您的OnException
方法将正确执行。The same rules apply for
Controller.OnException
as forHandleErrorAttribute
The required config for
HandleErrorAttribute
is noted here https://stackoverflow.com/a/528550/939634If you have
customError
in yourweb.config
set tomode="RemoteOnly"
and you are debugging locally or you havemode="Off"
the exceptions will not be handled and you will see the ASP.Net yellow screen with a stack trace.If you set
mode="On"
or you are viewing the site remotely and havemode="RemoteOnly"
yourOnException
method will execute properly.将
[HandleError]
属性应用于该类。Apply the
[HandleError]
attribute to the class.