Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net MVC [HandleError] not catching exceptions

People also ask

How can use HandleError attribute in MVC?

Add HandleError Attribute on About Action method of HomeController. Add shared folder in Views and then add an Error View (error. cshtml), which will be shown when an exception is thrown. We can also declare HandleError attribute on the Controller level as well as on Action method.

What is HandleError?

ASP.Net MVC has an attribute called "HandleError" that provides built-in exception filters. The HandleError attribute in ASP.NET MVC can be applied over the action method as well as Controller or at the global level. The HandleError attribute is the default implementation of IExceptionFilter.

How do you handle controller errors?

Another way to handle controller level exceptions is by overriding the OnException() method in the controller class. This method handles all your unhandled errors with error code 500. It allows you to log an exception and redirect to the specific view. It does not require to enable the <customErrors> config in web.


In Web.config, change customErrors:

<system.web>
  <customErrors mode="On">
  </customErrors>

If mode is either Off or RemoteOnly, then you will see the yellow screen of death instead of the custom error page. The reasoning is that developers usually want the more detailed information on the yellow screen of death.


Important: Be careful that your error page itself does not have an error on it!

If it does you'll end up with that ASP.NET custom error page and end up going round in circles and tearing your hair out. Just strip everything out of the page that could possibly cause an error and test it.

Also with respect to 'customErrors' being ON or OFF there are several contributing factors to whether or not the friendly error page (your Errors.aspx) page will be shown or not.

See this blog (except below)

HttpContext.IsCustomErrorEnabled - looks at three different sources

  1. The web.config's <deployment> section's retail property. This is a useful property to set when deploying your application to a production server. This overrides any other settings for custom errors.
  2. The web.config's <customErrors> section's mode property. This setting indicates whether custom errors are enabled at all, and if so whether they are enabled only for remote requests.
  3. The HttpRequest object's IsLocal property. If custom errors are enabled only for remote requests, you need to know whether the request is from a remote computer.

The idea here is that you can have 'customErrors' turned OFF during development - when you do want to see the errors, and then enable it for production only.

This MSDN article discusses the attribute further.


Another reason for this problem may be ,

In Template MVC Application (generated by VS2008 / VS2008 Express) , Error.aspx (generated by VS) uses Master Page.

If Master Page access any ViewData it will throw null reference Exception , then the error.aspx won't be shown.

Use this Simple code as your Error.aspx , it will solve the problem, (along with CustomErrors=On )

<%@ Page Language="C#"  Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>
<%= Model.Exception.Message %>

I have struggled with this as well and I believe I understand the problem now.

In short the requirements for having [HandleError] work as expected are:

You must enable custom errors in web.config AND you must also specify where your error view is in the <customErrors> tag.

Example:

<customErrors mode="On" defaultRedirect="Error" />

Leaving off the defaultRedirect="Error" part will instead yield a 500 error in the browser--NOT the ASP.NET error page (YSOD).

Also you do not have to be in Release mode. I tested this with a Debug build and it worked fine.

My environment was Visual Studio 2010 using .NET 4 and the standard, "ASP.NET MVC 2 Web Application" project template.

What confused me was the MSDN documentation for the HandleErrorAttribute Class. It doesn't explicitly say you must turn on custom errors in web.config. And I assumed all I needed was the [Handle Error] attribute.


There is some silly situation which once happened with me, so might be helpfull for someone.

Be sure that you've added <customErrors mode="On" /> to the correct web.config file.


Sometimes (especially, when you work with something like Resharper, and open your files with typing their name, but not via Solution Explorer), you can simply open a web.config either from Views folder or even from another project.


Watch out: in my case I was trying to get the HandleError attribute to catch an exception thrown inside the Controllers constructor! Of course it won't catch it. The HandleError attribute only catches exceptions thrown inside Controller actions. It's right there in the MSDN page (should've paid more attention to that):

Represents an attribute that is used to handle an exception that is thrown by an action method.

Another thing that was happening is that the Controller's OnException(ExceptionContext exceptionContext) overridden method was never being called. Again: of course it would not be called since I was throwing an exception inside the Controller's constructor.

I spent 1 hour trying to figure this out. :o) Hope it helps the next soul...

As a hint: remember that the HandleError attribute only catches 500 errors. For the other ones you should declare the <customErrors> section in Web.config:

<customErrors mode="On">
  <error statusCode="403" redirect="~/403" />
  <error statusCode="404" redirect="~/404" />
</customErrors>