ASP.NET中的錯誤處理有三個方面:
在本章中,我們將討論跟蹤,錯誤處理以及調(diào)試。
要理解這些概念,創(chuàng)建一個ASP.Net空網(wǎng)站項目:ErrorHandling 。 它有一個標簽控件,一個下拉列表和一個鏈接。 下拉列表加載名人名言的數(shù)組列表,所選引用顯示在下面的標簽中。它也有超鏈接,但是指向一個不存在的鏈接(僅作為示例演示)。參考以下代碼(Default.aspx) -
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>ASP.Net錯誤處理示例</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblheading" runat="server" Text="跟蹤,調(diào)試和錯誤處理">
</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.html">鏈接到:</asp:HyperLink>
</div>
</form>
</body>
</html>
以下是Default.aspx.cs 的代碼 -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
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("{1}, 名言: {0}", ddlquotes.SelectedItem.Text, ddlquotes.SelectedValue);
}
}
}
運行上面示例代碼,得到以下結果 -

要啟用頁面級別跟蹤,需要修改Page指令并添加Trace屬性,如下所示:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Trace ="true"%>
現(xiàn)在當執(zhí)行這個文件時,就會得到以下跟蹤信息:

它在頂部提供以下信息:
每次請求頁面時,服務器發(fā)送的狀態(tài)碼顯示錯誤的名稱和時間(如果有的話)。 下表顯示了常見的HTTP狀態(tài)代碼:
| 狀態(tài)碼 | 描述 |
|---|---|
| 100 | 繼續(xù) |
| 101 | 切換協(xié)議 |
| 200 | 完成 |
| 204 | 無內(nèi)容 |
| 301 | 永久轉移 |
| 305 | 使用代理 |
| 307 | 臨時重定向 |
| 400 | 錯誤的請求 |
| 402 | 需要抵消 |
| 404 | 未找到 |
| 408 | 請求超時 |
| 417 | 未實現(xiàn)預期 |
| 500 | 內(nèi)部服務器錯誤 |
| 503 | 服務不可用 |
| 505 | HTTP版本不受支持 |
在頂級信息下面有Trace日志,提供頁面生命周期的細節(jié)。它提供自頁面初始化以來經(jīng)過的時間(秒)。如下圖所示 -
下一個信息塊是控制樹,它以分層的方式列出頁面上的所有控件:

最后在會話和應用程序狀態(tài)摘要,Cookie和標題集合之后列出所有服務器變量。
跟蹤對象允許將自定義信息添加到跟蹤輸出。 它有兩個方法來完成這個操作:Write方法和Warn方法。
更改Page_Load事件處理程序以使用Write方法記錄程序執(zhí)行過程:
Trace.Write("頁面已經(jīng)開始加載...");
if (!IsPostBack)
{
Trace.Write("Not Post Back, Page Load");
......
運行觀察效果:

要使用Warn方法,可在選擇的索引更改的事件處理程序中強制輸入一些錯誤的代碼:
// 強制拋出錯誤
try
{
int a = 0;
int b = 9 / a;
}catch (DivideByZeroException e1)
{
Trace.Warn("UserAction", "processing 9/a", e1);
}
Try-Catch是一個C# 編程結構。 try塊保存任何可能產(chǎn)生錯誤或者不產(chǎn)生錯誤的代碼,catch塊捕獲錯誤。 程序運行時,會在跟蹤日志中發(fā)送警告。

應用程序級別跟蹤適用于網(wǎng)站中的所有頁面。 它通過在web.config文件中放入以下代碼行來實現(xiàn):
<system.web>
<trace enabled="true" />
</system.web>
雖然ASP.NET可以檢測到所有的運行時錯誤,但仍然有一些細微的錯誤。 通過跟蹤觀察錯誤是為了方便開發(fā)人員發(fā)現(xiàn)程序問題,而不是為了用戶。
因此,為了截獲這種情況,可以在應用程序的web.config文件中添加錯誤處理設置。 這是應用程序范圍的錯誤處理。 例如,可以在web.config文件中添加以下行:
<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.html">
<error statusCode="403" redirect="NoAccess.html" />
<error statusCode="404" redirect="FileNotFound.html" />
</customErrors>
</system.web>
<configuration>
<customErrors>部分可能有的屬性:
為了針對不同類型的錯誤放置不同的自定義錯誤頁面,根據(jù)錯誤的狀態(tài)代碼使用<error>子標記,其中指定了不同的錯誤頁面。
要實現(xiàn)頁面級錯誤處理,可以修改Page指令:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="errorhandling._Default" Trace ="true" ErrorPage="PageError.html" %>
由于ASP.NET調(diào)試本身是一個重要的主題,因此在接下來的教程中,將在單獨一篇文章討論它。