views:

568

answers:

3

I know that in ASP.NET people typically put a protected method called "Application_Error" in their global.asax file inorder to handle say, logging of exceptions.

My question is, is this an ASP.NET only thing, or does it work equally well in ASP.NET MVC? Is it an ASP.NET MVC best practice or not?

+3  A: 

You should be using the Exception Filter to handle exceptions in your controllers. You do this by adding the [HandleError] attribute to the controller action. Further information can be found at: on the MS MVP MVC Blog

Wolfwyrd
A: 

You can use the Application_Error block but better yet, use ELMAH. It's a lovely error logging tool (and I'm kind of surprised that it hasn't been bundled in with ASP.Net/ASP.Net MVC).

If you've never used it, it captures the yellow screen of death and a whole mess of environmental variables so that you can review the errors that you've caught.

Along with this, all of our controllers inherit from a controller base class that's decorated with the [HandleError] attribute that Wolfwyrd linked to so that we get the best of both worlds.

48klocs
A: 

Anything that works in ASP.Net works in ASP.Net MVC, and I do mean anything. Including your old-style ASPX pages complete with code-behind and Viewstate.

Cyril Gupta