(可视)Web 部件的顶级异常处理

发布于 2024-09-19 20:12:23 字数 540 浏览 10 评论 0原文

我对此进行了相当广泛的研究,并发现了以下内容:

  1. 您可以通过实现基类做一些聪明的事情来捕获大多数错误,请参阅 Andreas Knudsen 的解决方案。
  2. UserControl 中的 Error 事件永远不会被触发,请在此处查看详细信息: http ://weblogs.asp.net/vga/archive/2003/06/16/8748.aspx

我找不到捕获回发事件中发生的错误的任何通用方法,例如点击事件Web 部件或用户控件级别的按钮。我所说的一般是指我可以在基类中实现的东西。

我知道我应该在代码中进行适当的 try/catch,但对于大型团队,我想确保 Web 部件不会使页面崩溃,但始终显示一条不错的消息并允许其他部件继续执行页面上的 Web 部件。

我认为这是不可能的,但我希望被证明是错误的。

谢谢, 比约恩

I've looked into this quite extensively and I've found the following:

  1. You can do some clever stuff to catch most errors by implementing a base class, see Andreas Knudsen's solution for this.
  2. The Error event in UserControl never gets fired, see details here: http://weblogs.asp.net/vga/archive/2003/06/16/8748.aspx

What I can't find is any general way to catch errors occurring in postback events, such as the click event for a button, at the web part or user control level. What I mean by general is a something I can implement in a baseclass.

I'm aware the I should do proper try/catch in my code, but for large teams I'd like to be sure that a web part never crashes the page, but always shows a nice message and allows execution to continue for the other web parts on the page.

I don't think it's possible but I'd love to be proved wrong.

Thanks,
Bjoern

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

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

发布评论

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

评论(1

寄与心 2024-09-26 20:12:27

我认为这是可能的,但是您必须解决这样一个事实:当 asp.net 分发回发事件时,它不会关心您的用户控件定义,而是它只关心显式实现 IPostBackEventHandler 的控件(例如按钮/下拉列表等)

对于页面,您可以像其他方法一样覆盖并尝试/捕获 RaisingPostBackEvent 。 (如果你曾经想要一个通用的页面异常处理设置。(如果你想要这个,那么请重新考虑。我在上一个项目中花了太多时间基本上重新实现默认的 ASP.NET 异常处理逻辑只是为了让它工作。魔鬼在细节中)

您可以做的是在系统中拥有一个基本页面,所有页面都继承该基本页面并覆盖 RaisePostBackEvent(source, eventArgs) 在此方法中,您可以查看源是否继承自异常处理基本控件,或者如果它包含在执行此操作的控件中(导航父图形)。
如果它包含在一个中,则在对 base.Raise 的调用周围执行 try/catch....(请参阅中的代码
asp.net 的透明通用异常处理/ MOSS2007(带有代码) ),如果确实发生任何异常,则对找到的第一个候选者调用异常发生方法。

I think this is possible, but you have to work around the fact that when asp.net distributes the postback events it couldn't care less about your user control definitions, instead it only cares about controls that explicitly implement IPostBackEventHandler (like button / dropdown etc)

For pages you can override and try/catch RaisedPostBackEvent like for the other methods. (if you ever wanted a generic page-exception handling setup. (if you want this then please reconsider. I spent way too much time in my last project basically reimplementing the default asp.net exception handling logic just to get this to work. the devil's in the details)

What you could do is to have a base page in your system which all pages inherit from and which overrides RaisePostBackEvent(source, eventArgs). In this method you could see if the source inherits from your exception handling base control, or if it is contained within a control which does this. (navigate the parent graph)
If it is contained by one then do a try/catch around the call to base.Raise.... (see the code in
Transparent generic exception handling for asp.net / MOSS2007 (with code) ) and call the exceptionhappened method on the first candidate you found if indeed any exceptions occur.

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