What is the best practice for handling global errors / exceptions in ASP.NET MVC?

I saw two methods for implementing global error handling in an ASP.NET MVC 3 application. One method is implemented using 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 ... } } 

Another method is through the filter attribute [HandleError] registered in the RegisterGlobalFilters method, again in Global.asax.cs .

What is the best way to approach this? Are there any significant flaws for any approach?

+4
source share
1 answer

[HandleError] is the way to go because it keeps everything simple and the responsibility is clear. This action filter is specific to ASP.NET MVC and therefore is the official way to handle errors. It is also quite easy to override the filter to add custom functions.

Application_Error is an old way to do this and does not actually belong to MVC.

The [HandleError] attribute works fine until you remember it with controllers (or the base controller).

Update :

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

+8
source

All Articles