views:

2052

answers:

4

I am attempting to implement gobal error handeling, in my MVC application.

I have some logic inside my Application_Error that redirects to an ErrorController, but its not working.

I have a break point inside my Application_Error method in the the Global.aspx.

When I force an exception the break point is not being hit. Any idea why?

+2  A: 

Debugger just can't step into this method (instead it should break on a line which caused an exception). Use this for testing:

protected void Application_Error(object sender, EventArgs e)
{
    var error = Server.GetLastError();
    Server.ClearError();
    Response.ContentType = "text/plain";
    Response.Write(error ?? (object) "unknown");
    Response.End();
}

Web.config

<customErrors mode="Off" />
Koistya Navin
No! thats a poor idea. I want the global error handling, to log the error and present a friendly screen to the user.
Dan
Your links are not really relevant to my question.
Dan
@Dan, you have not get my point. Debugger just can't step into your Application_Error method. Use my code without debugger and you will see that it works. BTW, you beat off any desier to help you.
Koistya Navin
Sorry - its not my attention to offend, just thought your initial answer was not up to scratch\clear.
Dan
A: 

I can't tell you for sure what's wrong, but I can think of a couple of things to check... firstly, is VS breaking on the exception? If you are in the debugger this is the default I think. If it is just hit F5 until you get to your code with the breakpoint. Also, are you sure you don't handle the exception anywhere before it gets to Application_Error?

Another thing to check - CustomErrors mode in web.config, this is set to Off, right?

Steve Haigh
No don't handle it elsewhere, im just getting an ungraceful standard asp.net error page.
Dan
OK thanks, added another thought to my post.
Steve Haigh
A: 

I think a better way to handle this would be using the HandleErrorAttribute to decorate your controller (perhaps a base controller). This would give you the option to do logging or handle errors in different controllers with different errors by extending this attribute and modifying it to fit your needs, say by changing the view that gets rendered. Using this attribute uses the standard filter processing in MVC and builds the page using views rather than writing directly to the response as you might do using Application_Error.

tvanfosson
Using the HandleError attribute doesn't help with errors that happen outside controllers as far as I can tell (for example in RegisterRoutes in Global.asax.cs).
Michael Maddox
@Michael - true, it only applies to a controller or controller method since the framework only recognizes the attributes there. In practice I don't find this to be a problem. For me, the only code that runs outside of an action is done at start up. I use ELMAH with a custom HandleError attribute anyway so both of my bases are covered.
tvanfosson
+5  A: 

Actually the debugger CAN step into the Application_Error method.

Chris