ErrorAttribute vs OnException vs Application_Error
我想处理应用程序范围的错误并在 asp.net mvc 中显示 ErrorView 页面。 有 3 种方法可以做到这一点(或者我知道)。
1) ErrorAttribute in BaseController:Controller class.
Can be used on individual Action/Controller/BaseController.
2) Override OnException() in the BaseController:Controller class.
Will work on Controllers derived from BaseController
3) Application_Error in Global_aspx.
最好的做法是什么。 应使用这些方法中的哪一种来进行应用程序范围的错误处理,或者我们应该使用多种方法还是只使用一种方法。
如果我们在 BaseController 上的 ErrorAttribute 或 OnException() 上处理错误,我们是否仍然应该在 Application_Error() 中处理它。
我们什么时候应该使用Application_Error()?
I want to handle application wide error and show a ErrorView page in asp.net mvc.
There are 3 ways to do it (or i know).
1) ErrorAttribute in BaseController:Controller class.
Can be used on individual Action/Controller/BaseController.
2) Override OnException() in the BaseController:Controller class.
Will work on Controllers derived from BaseController
3) Application_Error in Global_aspx.
What is the best practice.
Which one of these methods should be used for application wide error handling or should we use multiple or only one.
If we handle error on ErrorAttribute Or/And OnException() on BaseController should we still handle it in Application_Error().
When should we use Application_Error()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HandleErrorAttribute
是通过属性应用的 MVC 过滤器。您可以提供一个视图名称以在发生异常时显示,还可以指定此过滤器适用的异常的基本(或特定)类型。如果未提供视图名称,它将查找名为“Error”的视图。正如您已经注意到的,您可以将其应用于各种范围。它允许您根据异常指定不同的“错误页面”视图。Controller.OnException
是一个方法,如果您的任何操作最终抛出错误,就会调用该方法。以上两者都是 MVC 概念,也是 MVC 管道的一部分,它位于 ASP.NET 管道的顶部,如果您使用上述方法处理异常,它不会传播到
Application_Error,但是像 http 错误 404、500 和 will,如果我没记错的话。
用什么?
一定要研究 ELMAH 以进行应用程序范围的错误日志记录和我的 关于 ELMAH 和 ASP.NET MVC 的博客文章
关于显示错误页面,您应该只使用
[HandleError]
和HandleErrorAttribute
,因为它已经为您处理了所有事情(每个异常类型的可选过滤和可选自定义错误页面)。HandleErrorAttribute
is an MVC filter applied via the attribute. You can supply a view name to display if an exception occurs and you can also specify the base (or specific) type of exception this filter applies to. If no view name is supplied it will look for a view named "Error". As you've already noticed you can apply it to various scopes. It allows you to specify a different "error page" view based on the exception.Controller.OnException
is a method that will get called if any of your actions ends up throwing an error.Both of the above two are MVC concepts and part of the MVC pipeline, which sits on top of the ASP.NET pipeline, and if you handle the exception using the above it won't propagate to
Application_Error
, but things like http errors 404, 500 and will if I remember correctly.What to use?
Definitely look into ELMAH for application wide error logging and my blog post about ELMAH and ASP.NET MVC
Regarding displaying error pages you should be fine with just using
[HandleError]
and theHandleErrorAttribute
, since it already handles everything for you (optional filtering and optional custom error page per exception type).如果您想在应用程序级别处理错误,则不要对控制器应用 HandleError 或 OnException Override。
尝试从 Application_Error 处理程序中的服务器对象获取最后一个错误,检查异常类型并根据异常类型定义您想要执行的操作。
对于 404,您可能希望在控制器上设置不同的操作来处理。
对于 500,您可能希望在控制器上设置不同的操作来处理。
对于NON HTTPException (SQLException) 您甚至可能想发送电子邮件。
请确保您出于 SEO 目的设置正确的响应状态代码。
if you want to handle the error on application level then don't apply the HandleError or OnException Override for the controller.
Try to Get the Last Error from the Server Object in Application_Error Handler check the Exception Type and based on the exception type define the action you would like to perform.
For 404 you might want to set a different action on the controller to handle.
For 500 you might want to set a different action on the controller to handle.
For NON HTTPException (SQLException) you might even want to send out email.
Please make sure you set the correct Response Status Code for SEO purpose.