Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Global error handling: Application_Error not firing

I am attempting to implement global error handling in my MVC application.

I have some logic inside my Application_Error that redirects to an ErrorController but it's 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 ideas why?

like image 831
Dan Avatar asked Apr 05 '09 17:04

Dan


2 Answers

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" />
  • Rich Custom Error Handling with ASP.NET
  • How to: Handle Application-Level Errors
like image 77
Konstantin Tarkus Avatar answered Sep 22 '22 23:09

Konstantin Tarkus


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.

like image 35
tvanfosson Avatar answered Sep 22 '22 23:09

tvanfosson