16

I want to handle application wide error and show a ErrorView page in asp.net mvc. There are 3 ways to do it (or i know).

1) ErrorAttribute in BaseController:Controller class.
     Can be used on individual Action/Controller/BaseController.
2) Override OnException() in the BaseController:Controller class.
     Will work on Controllers derived from BaseController
3) Application_Error in Global_aspx.

What is the best practice. Which one of these methods should be used for application wide error handling or should we use multiple or only one.

If we handle error on ErrorAttribute Or/And OnException() on BaseController should we still handle it in Application_Error().

When should we use Application_Error()?

Joel Coehoorn
  • 362,140
  • 107
  • 528
  • 764
Mangesh Pimpalkar
  • 3,937
  • 2
  • 29
  • 51

2 Answers2

11
  1. HandleErrorAttribute is an MVC filter applied via the attribute. You can supply a view name to display if an exception occurs and you can also specify the base (or specific) type of exception this filter applies to. If no view name is supplied it will look for a view named "Error". As you've already noticed you can apply it to various scopes. It allows you to specify a different "error page" view based on the exception.

  2. Controller.OnException is a method that will get called if any of your actions ends up throwing an error.

  3. Both of the above two are MVC concepts and part of the MVC pipeline, which sits on top of the ASP.NET pipeline, and if you handle the exception using the above it won't propagate to Application_Error, but things like http errors 404, 500 and will if I remember correctly.

What to use?

Definitely look into ELMAH for application wide error logging and my blog post about ELMAH and ASP.NET MVC

Regarding displaying error pages you should be fine with just using [HandleError] and the HandleErrorAttribute, since it already handles everything for you (optional filtering and optional custom error page per exception type).

Ivan Zlatev
  • 11,978
  • 8
  • 38
  • 48
  • If I have HandleErrorAttribute capturing the Exception class, doesn't that prevent all "unhandled exceptions?" If so, doesn't that prevent Elmah from working? – Brent Arias May 15 '12 at 23:24
  • Yes it does, but you can have a filter that runs before everything that logs the exception through Elmah but does not handle it or suppress it - http://stackoverflow.com/questions/766610/how-to-get-elmah-to-work-with-asp-net-mvc-handleerror-attribute/5936867#5936867 – Ivan Zlatev May 16 '12 at 11:11
  • That last paragraph has to be wrong, or I'm misunderstanding something. For displaying error pages, how is it fine to just use the `HandleErrorAttribute`? ELMAH doesn't do anything for handling the error, it just logs it, and with the `HandleErrorAttribute` any error that isn't 500 or happens outside of the controller won't be handled. So you would also need to use `Application_Error`. – wired_in Feb 23 '14 at 22:47
  • @wired_in I simply mentioned ELMAH for error logging and not handling. Also you are right in that the HandleErrorAttribute will not capture errors outside of the ASP.NET pipeline. – Ivan Zlatev Feb 25 '14 at 18:35
  • 1
    @IvanZlatev so doesn't "Regarding displaying error pages you should be fine with just using `[HandleError]` and the `HandleErrorAttribute`" need to be revised? – wired_in Feb 25 '14 at 18:41
  • @IvanZlatev and it's worse than just not capturing errors outside of the asp.net pipeline... It won't capture errors outside of the action and it's action filters, much more of a narrow scope. – wired_in Feb 25 '14 at 18:44
  • Meant to say ASP.NET *MVC* pipeline. – Ivan Zlatev Feb 26 '14 at 11:47
0

if you want to handle the error on application level then don't apply the HandleError or OnException Override for the controller.

Try to Get the Last Error from the Server Object in Application_Error Handler check the Exception Type and based on the exception type define the action you would like to perform.

For 404 you might want to set a different action on the controller to handle.

For 500 you might want to set a different action on the controller to handle.

For NON HTTPException (SQLException) you might even want to send out email.

Please make sure you set the correct Response Status Code for SEO purpose.

Amit Bagga
  • 650
  • 3
  • 11