Showing posts with label Application_Error. Show all posts
Showing posts with label Application_Error. Show all posts

Saturday, September 17, 2016

Application_Error not firing in ASP.NET MVC

Application_Error not firing in ASP.NET MVC


If you want to handle errors from single location, Global.asax is the best place to handle it. You can get the error, log it and inform the user about error in a custom way, but what if the Application_Error is not getting fired on error event?

Yes I faced this issue and after doing google I found few causes which prevents the Application_Error event.

1) Set the  customErrors = "On" in web.config

2) comment the filters.Add(new HandleErrorAttribute()); in RegisterGlobalFilters().
 
public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute()); // this line is the culprit
    }
...
}
 When you create new MVC project the HandleErrorAttribute is by default added in
GlobalFilters which prevent Application_Error event to be fired. So just comment 
that line in FilterConfig.cs file which is under App_Start directory.