Should you use the method Application_Error in your global.asax in ASP.NET MVC?

倾然丶 夕夏残阳落幕 提交于 2020-01-04 06:18:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!