(可视)Web 部件的顶级异常处理
我对此进行了相当广泛的研究,并发现了以下内容:
- 您可以通过实现基类做一些聪明的事情来捕获大多数错误,请参阅 Andreas Knudsen 的解决方案。
- 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:
- You can do some clever stuff to catch most errors by implementing a base class, see Andreas Knudsen's solution for this.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是可能的,但是您必须解决这样一个事实:当 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.