ASP.NET MVC Controller.OnException 未被调用

发布于 2024-07-23 12:11:57 字数 754 浏览 6 评论 0原文

我有一个基本控制器类,我在其中重写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

水水月牙 2024-07-30 12:11:57

如果我正确理解你的问题 - 你的异常必须在 OnException 中标记为“已处理”。 尝试这个:

filterContext.ExceptionHandled = true;

If I understand your question correctly - your Exception must be marked as "handled" in your OnException. Try this:

filterContext.ExceptionHandled = true;
锦欢 2024-07-30 12:11:57

MSDN 文档 (http://msdn. microsoft.com/en-us/library/system.web.mvc.controller.oneException.aspx) 指出 OnException 方法“在操作中发生未处理的异常时调用”。

因此,听起来它可能只会在操作方法中发生未处理的异常时触发。

我创建了一个带有一个 Index 操作的新控制器。 OnException 方法实际上会执行。 我还尝试抛出 SystemException() 并触发 OnException 方法。

    public ActionResult Index ( )
    {
        throw new NotImplementedException ( );
    }

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.

    public ActionResult Index ( )
    {
        throw new NotImplementedException ( );
    }
甚是思念 2024-07-30 12:11:57

我在 ASP.NET MVC 3 项目中遇到了完全相同的问题。

原来这是Visual Studio和开发服务器的一个功能。 当处于调试模式时,您将返回到 Visual Studio 并给出默认的异常对话框。

阻止这种情况发生的一种方法是更改​​ Web.config 中的编译模式:

<compilation debug="true" targetFramework="4.0">

改为:

<compilation debug="false" targetFramework="4.0">

您还可以将调试模式设置为 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:

<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.

国际总奸 2024-07-30 12:11:57

您是从单元测试还是通过 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.

我的影子我的梦 2024-07-30 12:11:57

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 for HandleErrorAttribute

The required config for HandleErrorAttribute is noted here 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.

If you set mode="On" or you are viewing the site remotely and have mode="RemoteOnly" your OnException method will execute properly.

樱花落人离去 2024-07-30 12:11:57

[HandleError] 属性应用于该类。

Apply the [HandleError] attribute to the class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文