问题
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?
回答1:
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
回答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.
回答3:
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.
来源:https://stackoverflow.com/questions/1194578/should-you-use-the-method-application-error-in-your-global-asax-in-asp-net-mvc