4

I've seen two methods of implementing global error handling in an ASP.NET MVC 3 application. One method is via the Application_Error method in Global.asax.cs.

For example (Error Handling in global.asax):

public class SomeWebApplication : System.Web.HttpApplication {

  // ... other methods ...

  protected void Application_Error() {
    // ... application error handling code ...
  }
}

The other method is via a [HandleError] action filter attribute registered in the RegisterGlobalFilters method, again in Global.asax.cs.

Which is the better way to approach this? Are there any significant disadvantages to either approach?

1 Answer 1

8

[HandleError] is the way to go since it keeps everything simple and responsibility is clear. This action filter is a specific ASP.NET MVC feature and therefore is the official way of handling errors. It's also quite easy to override the filter to add custom functionality.

Application_Error is the old way to do it and doesn't really belong in MVC.

The [HandleError] attribute works fine as long as you remember to tag your controllers (or the base controller) with it.

Update:

Created a blog entry: http://blog.gauffin.org/2011/11/how-to-handle-errors-in-asp-net-mvc/

Sign up to request clarification or add additional context in comments.

5 Comments

an error controller and using the default <customErrors> section in web.config.
@PsychoDad: customErrors in web.config and an error controller.
customErrors redirecting which is not quite well, how to just change view and leave 404 status code?
@dan: customErrors in web.config (defaultRedirect) handles non-404 exceptions outside of the MVC pipeline. What about AJAX errors? Selective Result returned based on request type?
Your blog link gives a connection timeout

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.