ASP.NET MVC Controller.OnException not being called
Asked Answered
R

6

10

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();
        }
}
Rubicund answered 6/6, 2009 at 18:42 Comment(2)
"The OnException method never gets called when an exception gets raised by the controller" - did you check this with debugger? It's not clear from your question.Saucier
I've tried it from the debugger and by using log statements and running it outside of the debugger. Neither of them seem to work for me.Rubicund
S
12

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

filterContext.ExceptionHandled = true;
Saucier answered 6/6, 2009 at 20:32 Comment(1)
minus 1. doesn't answer the question at all.Lanell
M
2

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 ( );
    }
Mooring answered 16/6, 2009 at 20:37 Comment(0)
D
2

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.

Discontented answered 28/11, 2012 at 12:26 Comment(0)
M
1

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.

Mezereum answered 1/10, 2009 at 15:40 Comment(0)
K
0

The same rules apply for Controller.OnException as for HandleErrorAttribute

The required config for HandleErrorAttribute is noted here https://mcmap.net/q/976961/-attribute-does-not-seem-to-act-at-all

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.

Kutch answered 9/3, 2018 at 18:39 Comment(0)
A
-1

Apply the [HandleError] attribute to the class.

Anew answered 6/6, 2009 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.