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?
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" />
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With