何时覆盖 OnError?

发布于 2024-09-05 07:32:50 字数 416 浏览 15 评论 0原文

我正在考虑在我支持的应用程序中重新设计和简化错误处理。目前,我们所有的页面都继承自我们创建的基类,而该基类显然又继承自 System.Web.UI.Page。在此基类中,OnError 方法当前被重写,并依次调用 MyBase.OnError,然后调用我们的自定义日志记录方法之一。

我没有看到重写 OnError 方法有任何好处,我认为最好让 Global.asax 中的 Application_Error 方法处理未处理的异常(记录它),然后配置中的 customErrors 部分将触发重定向用户的过程。

在网上看,人们似乎经常覆盖此方法,但我认为没有必要,并且 MSDN 上的这篇文章让我也有同样的想法。

I'm looking into re-working and simplifying our error handling in an application I support. We currently have all of our pages inheriting from a base class we created, which in turn obviously inherits from System.Web.UI.Page. Within this base class, the OnError method is currently being overridden, and in turn calling MyBase.OnError, and then calling one of our custom logging methods.

I don't see any benefit of overriding the OnError method, and I think it would be better to let the Application_Error method in the Global.asax take care of the unhandled exception (logging it) and then the customErrors section in the config would trigger a process to redirect the user.

Looking online it looks like people override this method quite frequently, but I don't see a need to and this article from MSDN makes me think the same.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花开半夏魅人心 2024-09-12 07:32:50

我创建了一个名为 PageBase 的自定义类:

public class PageBase : Page
{
  protected override void OnError(..)
  {
     //handle error, redirect to error page
  }
}

因此我只需执行一次,然后使用它来捕获未处理的错误并重定向到错误页面。这样我就必须做一次;我不知道 Page.Error 事件相对于应用程序错误有什么优点或缺点;但我使用页面错误,因为它在这里很方便;我可以清除错误并在页面上下文中重定向到错误页面...我个人的偏好。

感谢您提供 MSDN 链接;那是一个非常好的资源。

HTH。

I create a custom class called PageBase:

public class PageBase : Page
{
  protected override void OnError(..)
  {
     //handle error, redirect to error page
  }
}

And so I only have to do it once, and use that to catch unhandled errors and redirect to the error page. This way I have to do it once; I don't know that Page.Error event has any pros or cons over application error; but I use the page error as it can be convenient here; I can clear the error and redirect to the error page right within the context of the page... my personal preference.

Thanks for the MSDN link; that was a pretty good resource.

HTH.

苍暮颜 2024-09-12 07:32:50

我从未重写过 OnError 方法。我喜欢使用全局 asax 的 Application_Error ,它反过来会捕获可能不是从基类继承的任何页面。此外,重写方法用于更改其功能,因此如果您不这样做,我就不会重写它。

另外,我知道这不是您问题的一部分,但我会查看 ELMAH 的错误日志记录:

http: //code.google.com/p/elmah/

I have never overriden the OnError method. I like to use the Application_Error of the global asax, which in turn will catch any pages that might not inherit from your base class. Furthermore, overriding a method is used to change it's functionality, so if you're not doing so I wouldn't override it.

Also, I know it's not part of your question, but I would look at ELMAH for error logging:

http://code.google.com/p/elmah/

笑叹一世浮沉 2024-09-12 07:32:50

我可以看到这样的场景:应用程序中可能只有某些页面继承自基类,并且需要以不同的方式处理错误。所有其他错误将被 Application_Error 捕获/记录

I could see a scenario where maybe only some pages in the application inherit from the base class and need to handle errors differently. All other errors would be caught/logged by Application_Error

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文