2

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?

Community
  • 1
  • 1
KingNestor
  • 65,976
  • 51
  • 121
  • 152
  • You can use the Application_Error() method but it won't work as you expect. Here is another SO question where we are discussing the problem further: http://praveenbattula.blogspot.com/2009/12/iis-7-managed-pipeline-mode-globalasax.html – Jesse Webb Jul 27 '11 at 21:09

3 Answers3

5

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
  • 15,716
  • 5
  • 47
  • 67
2

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
  • 6,073
  • 3
  • 27
  • 34
-2

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
  • 13,505
  • 11
  • 64
  • 87
  • 1
    Everything from ASP.NET is _supposed_ to work in MVC3. Unfortunately, one of the things that doesn't have the same behavior between frameworks is the Application_Error() method: http://praveenbattula.blogspot.com/2009/12/iis-7-managed-pipeline-mode-globalasax.html. – Jesse Webb Jul 27 '11 at 21:07
  • This is completely untrue and misleading. There is plenty of stuff that doesn't work in MVC. Another example is Server.Transfer. – Carvellis Oct 24 '12 at 18:59