MVC Global error handling: Application_Error not firing
Solution 1
You can try this approach 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" />
Solution 2
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.
Dan
Updated on July 09, 2022Comments
-
Dan almost 2 years
I am attempting to implement global error handling in my MVC application.
I have some logic inside my
Application_Error
that redirects to anErrorController
but it's not working.I have a break point inside my
Application_Error
method in the theGlobal.aspx
.When I force an exception the break point is not being hit. Any ideas why?
-
Dan about 15 yearsNo! thats a poor idea. I want the global error handling, to log the error and present a friendly screen to the user.
-
Dan about 15 yearsYour links are not really relevant to my question.
-
Konstantin Tarkus about 15 years@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.
-
Dan about 15 yearsSorry - its not my attention to offend, just thought your initial answer was not up to scratch\clear.
-
Dan about 15 yearsNo don't handle it elsewhere, im just getting an ungraceful standard asp.net error page.
-
Admin over 14 yearsActually the debugger CAN step into the Application_Error method.
-
Michael Maddox almost 14 yearsUsing 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).
-
tvanfosson almost 14 years@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.
-
Crwydryn over 8 yearsMarked down because the debugger CAN step into Application_Error as suggested by @user181328
-
Clarence over 6 years@tvanfosson same for ajax calls, also. It goes straight to the global.asax application_error if you have it... after some research and testing, I've decided upon a three prong solution. A. custom attribute using RegisterGlobalFilters not decorated controllers B. Updating web.config to turn custom error handling on C. JavaScript that captures all messages, either using the model and a helper method or direct from the ajax status code and a redirect to the error page via javaScript whereby the model and the ajax funnel their error into