HandleError attribute doesn't have any effect

mosquito87 picture mosquito87 · Jul 29, 2013 · Viewed 8.4k times · Source

In my web.config I have included:

<customErrors mode="On" />

Now the yellow screen of death isn't shown anymore. I thought I'd have to include the HandleError attribute to my controller methods or the class itself:

[HandleError]
public ActionResult About()
{
    throw new Exception("Just an exception");
    return View();
}

But it doesn't have any effect, it's the same as:

public ActionResult About()
{
    throw new Exception("Just an exception");
    return View();
}

In both cases the custom error page is shown. So what is it about the HandleError attribute?

Answer

Amna Ali picture Amna Ali · Dec 26, 2013

That can happen if FilterConfig.cs, under App_Start folder of the MVC project, contains:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
}

Since the HandleError filter is registered when the App starts, you don't have to decorate each controller action with this attribute.