ASP.NET中的错误处理包括三个方面:
在本章中,我们将讨论跟踪和错误处理,在本章中,我们将讨论调试。 要理解这些概念,请创建以下示例应用程序。它具有标签控件,下拉列表和链接。下拉列表将加载著名报价的数组列表,并且所选报价显示在下面的标签中。它还具有指向不存在的链接的超链接。 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="errorhandling._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title> Tracing, debugging and error handling </title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="lblheading" runat="server" Text="Tracing, Debuggin and Error Handling"> </asp:Label> <br /> <br /> <asp:DropDownList ID="ddlquotes" runat="server" AutoPostBack="True" onselectedindexchanged="ddlquotes_SelectedIndexChanged"> </asp:DropDownList> <br /> <br /> <asp:Label ID="lblquotes" runat="server"> </asp:Label> <br /> <br /> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="mylink.htm">Link to:</asp:HyperLink> </div> </form> </body> </html> 文件后面的代码: public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string[,] quotes = { {"Imagination is more important than Knowledge.", "Albert Einsten"}, {"Assume a virtue, if you have it not" "Shakespeare"}, {"A man cannot be comfortable without his own approval", "Mark Twain"}, {"Beware the young doctor and the old barber", "Benjamin Franklin"}, {"Whatever begun in anger ends in shame", "Benjamin Franklin"} }; for (int i=0; i<quotes.GetLength(0); i++) ddlquotes.Items.Add(new ListItem(quotes[i,0], quotes[i,1])); } } protected void ddlquotes_SelectedIndexChanged(object sender, EventArgs e) { if (ddlquotes.SelectedIndex != -1) { lblquotes.Text = String.Format("{0}, Quote: {1}", ddlquotes.SelectedItem.Text, ddlquotes.SelectedValue); } } } 追踪要启用页面级跟踪,需要修改Page指令并添加Trace属性,如下所示: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="errorhandling._Default" Trace ="true" %> 现在,当您执行文件时,您将获得跟踪信息: 它在顶部提供以下信息:
每次请求页面时从服务器发送的状态代码都会显示错误的名称和时间(如果有)。下表显示了常见的HTTP状态代码:
在顶级信息下,有“跟踪日志”,其中提供了页面生命周期的详细信息。它提供自页面初始化以来经过的时间(以秒为单位)。 下一部分是控件树,它以分层方式列出页面上的所有控件: 会话和应用程序状态摘要,cookie和标头集合中的最后一个,其后是所有服务器变量的列表。 跟踪对象允许您将自定义信息添加到跟踪输出。它有两种方法可以实现此目的:Write方法和Warn方法。 更改Page_Load事件处理程序以检查Write方法: protected void Page_Load(object sender, EventArgs e) { Trace.Write("Page Load"); if (!IsPostBack) { Trace.Write("Not Post Back, Page Load"); string[,] quotes = ....................... } } 运行观察效果: 为了检查Warn方法,让我们在所选的索引已更改事件处理程序中强制输入一些错误代码: try { int a = 0; int b = 9 / a; }catch (Exception e) { Trace.Warn("UserAction", "processing 9/a", e); } Try-Catch是一种C#编程结构。try块包含可能会或可能不会产生错误的任何代码,并且catch块会捕获错误。程序运行时,它将在跟踪日志中发送警告。 应用程序级别跟踪适用于网站中的所有页面。通过将以下代码行放入web.config文件中来实现此功能: <system.web> <trace enabled="true" /> </system.web> 错误处理尽管ASP.NET可以检测到所有运行时错误,但是仍然可能存在一些细微的错误。通过跟踪观察错误是给开发人员的,而不是给用户的。 因此,要拦截这种情况,可以在应用程序的web.config文件中添加错误处理设置。它是应用程序范围的错误处理。例如,您可以在web.config文件中添加以下行: <configuration> <system.web> <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> <error statusCode="403" redirect="NoAccess.htm" /> <error statusCode="404" redirect="FileNotFound.htm" /> </customErrors> </system.web> <configuration> <customErrors>部分具有可能的属性:
要为不同类型的错误放置不同的自定义错误页面,请使用<error>子标记,其中根据错误的状态码指定了不同的错误页面。 为了实现页面级错误处理,可以修改Page指令: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="errorhandling._Default" Trace ="true" ErrorPage="PageError.htm" %> 因为ASP.NET调试本身就是一个重要的主题,所以我们将在下一章中单独讨论它。 |