Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HandleError attribute doesn't have any effect

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?

like image 845
mosquito87 Avatar asked Jul 29 '13 06:07

mosquito87


People also ask

What are the properties of handleerror attribute?

HandleError Attribute has a couple of properties which are useful in handling an exception and help in modifying the default behavior of HandleError Attribute. ExceptionType: Exception Type specifies the type of an exception. If not specified, then HandleError filter handles all the Exception.

How to handle the exception in MVC using handleerror attribute?

Today’s topic is how to handle the exception in ASP.NET MVC using HandleError attribute. In ASP.NET MVC, there are different ways to handle the exception. Generally we can use Try-Catch or some filters are available to handle the exception or sometimes we use configuration settings to show the relevant page when exception occurs.

How to handle [handleerror] in an action?

If you are going to use [HandleError] attribute, it means this error is applicable only for this action. First you need to decorate the action with this attribute, needs to be provide some other details like type of the exception and your View name.

What is the use of handleerror attribute in iexceptionfilter?

The HandleError attribute is the default implementation of IExceptionFilter. The HandleError filter handles the exceptions which been raised at the controller/actions. Then it return a custom error view. Let we discuss detail about with example. It catch only 500 Http error and not catch HTTP errors like 404,401 ..


1 Answers

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.

like image 169
Amna Ali Avatar answered Oct 17 '22 14:10

Amna Ali