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?
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.
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.
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.
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 ..
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.
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