简体   繁体   English

ASP.NET MVC Controller.OnException未被调用

[英]ASP.NET MVC Controller.OnException not being called

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). 我有一个基本控制器类,我将覆盖Controller.OnException处理程序方法,以便为将继承此类的某些类型的控制器提供一般错误处理(这些控制器将返回JSON结果)。 The OnException method never gets called when an exception gets raised by the controller. 当控制器引发异常时,永远不会调用OnException方法。 Does anyone see what I am doing wrong, or is there a better way to handle this situation? 有谁看到我做错了什么,或者有更好的方法来处理这种情况?

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();
        }
}

If I understand your question correctly - your Exception must be marked as "handled" in your OnException. 如果我正确理解您的问题 - 您的异常必须在OnException中标记为“已处理”。 Try this: 试试这个:

filterContext.ExceptionHandled = true;

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.onexception.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. 我创建了一个带有一个Index操作的新控制器。 And the OnException method does in fact execute. 并且OnException方法实际上确实执行。 I also tried throwing a SystemException() and the OnException method fired as well. 我也试过抛出一个SystemException()和OnException方法。

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

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. 事实证明,这是Visual Studio和开发服务器的一个功能。 When in debug mode you will be returned to Visual Studio and given the default exception dialog box. 在调试模式下,您将返回到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,但可以通过从visual studio菜单中选择Start without debugging option来测试您的应用程序(通过按Ctrl+F5调用)。

Are you calling the controller action from a unit test, or via ASP.NET? 您是通过单元测试还是通过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. 如果您在测试中直接调用该方法,比如通过NUnit调用,那么OnException将不会触发:测试框架将处理异常并将其转换为失败的测试。

The same rules apply for Controller.OnException as for HandleErrorAttribute 对于HandleErrorAttribute ,相同的规则适用于Controller.OnException

The required config for HandleErrorAttribute is noted here https://stackoverflow.com/a/528550/939634 HandleErrorAttribute所需的配置在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. 如果你的web.config customError设置为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方法将正确执行。

[HandleError]属性应用于类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ExceptionFilter之前调用Controller.OnException吗? - Does Controller.OnException get called before ExceptionFilter? Asp.net mvc 覆盖基础 controller 中的 OnException 不断传播到 Application_Error - Asp.net mvc override OnException in base controller keeps propagating to Application_Error ASP.NET MVC我可以有一个名为PropertiesController的控制器吗? - ASP.NET MVC Can I have a controller called PropertiesController? 获取在ASP.NET MVC中调用控制器的视图的名称 - Get the name of view who called controller in ASP.NET MVC ASP.NET MVC:控制器上的增量计数器,正在缓存 - ASP.NET MVC: Increment counter on controller, being cached 不调用ExceptionFilter OnException - ExceptionFilter OnException not being called 调用另一个控制器,并对调用的控制器操作MVC ASP.NET使用不同的布局 - Call another controller and use a different Layout for the called controller action MVC ASP.NET "ASP.NET Web App CORE:未调用控制器中的 HttpPost 编辑方法" - ASP.NET Web App CORE: the HttpPost Edit Method in Controller not being called Controller 动作方法未被调用在 ASP.NET Core 中发出发布请求 - Controller action method not being called making a post request in ASP.NET Core ASP.NET 核心 AuthorizationHandler 未被调用 - ASP.NET Core AuthorizationHandler not being called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM