ErrorAttribute vs OnException vs Application_Error
Asked Answered
P

2

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()?

Plumule answered 11/6, 2011 at 19:36 Comment(0)
V
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).

Virginiavirginie answered 11/6, 2011 at 21:15 Comment(7)
If I have HandleErrorAttribute capturing the Exception class, doesn't that prevent all "unhandled exceptions?" If so, doesn't that prevent Elmah from working?Mindoro
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 - #767110Virginiavirginie
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.Moorehead
@Moorehead 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.Virginiavirginie
@IvanZlatev so doesn't "Regarding displaying error pages you should be fine with just using [HandleError] and the HandleErrorAttribute" need to be revised?Moorehead
@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.Moorehead
Meant to say ASP.NET MVC pipeline.Virginiavirginie
U
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.

Unfathomable answered 12/6, 2011 at 7:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.