ASP.NET 3.5网站中错误处理的设计模式 [英] Design Pattern for error handling in ASP.NET 3.5 site

查看:209
本文介绍了ASP.NET 3.5网站中错误处理的设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对ASP.NET编程和Web编程一般比较新。我们有一个我们最近从.NET 1.1移植到3.5的站点。目前,我们有两种错误处理方法:在页面上的数据加载期间捕获错误,并在页面上的标签中显示格式化的错误,或重定向到通用错误页面。这两个都有点恼人,因为现在我正在尝试重新设计我们的错误是如何显示的。



我们很快就转到主页面,我想知道是否有一种方法来构建一个错误处理控件。我的意思是使用ASP.NET用户控件,我设计的只是传递从服务器返回的错误字符串。如果发生错误,页面将不会显示内容,而是显示错误控制。这为我们提供了在错误期间保留当前横幅/导航的能力(我们不会使用通用错误页面),并且让我无需将控件添加到我们拥有的每个aspx页面(我与使用每页标签系统有关)。这样的事情有意义吗?最终我只想把错误控制添加到一个页面,所有其他页面都可以直接访问它。这是Master页面有帮助吗?



谢谢!

解决方案

将global.asax中的application_error事件用于捕获所有未处理的错误 - 可以在这里记录。您不应该像这样向客户端输出错误消息。



使用web.config中的CustomErrors部分定义用户的自定义错误页面。



您还可以使用 ELMAH 易于登录。


I am relatively new to ASP.NET programming, and web programming in general. We have a site we recently ported from .NET 1.1 to 3.5. Currently we have two methods of error handling: either catching the error during data load on a page and displaying the formatted error in a label on the page, or redirecting to a generic error page. Both of these are somewhat annoying, as right now I'm trying to redesign how our errors are displayed.

We are soon moving to Master pages, and I'm wondering if there is a way to "build in" an error handling control. What I mean by this is using a ASP.NET user control I've designed that simply gets passed the error string returned from the server. If an error occurs, the page would not display the content, and instead display the error control. This provides us with the ability to retain the current banner/navigation during an error (which we don't get with the generic error page), as well as keeping me from having to add the control to every aspx page we have (which I have to do with using the label-per-page system). Does something like this make sense? Ultimately I just want to have the error control added to a single page, and all other pages have access to it directly. Is this something Master pages help with?

Thanks!

解决方案

User the application_error event in the global.asax to catch all your unhandled errors - you can log them here. You shouldn't be outputting error messages to the client like this.

Use the CustomErrors section in the web.config to define a custom error page for your users to get.

You could also look at using something like ELMAH for easy loggin.

这篇关于ASP.NET 3.5网站中错误处理的设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆