asp.net中的错误处理有三个方面:
在本章中,我们将讨论跟踪,错误处理以及调试。
要理解这些概念,创建一个asp.net空网站项目:errorhandling 。 它有一个标签控件,一个下拉列表和一个链接。 下拉列表加载名人名言的数组列表,所选引用显示在下面的标签中。它也有超链接,但是指向一个不存在的链接(仅作为示例演示)。参考以下代码(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="跟踪,调试和错误处理">
</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"%>
现在当执行这个文件时,就会得到以下跟踪信息:
它在顶部提供以下信息:
每次请求页面时,服务器发送的状态码显示错误的名称和时间(如果有的话)。 下表显示了常见的http状态代码:
状态码 | 描述 |
---|---|
100 | 继续 |
101 | 切换协议 |
200 | 完成 |
204 | 无内容 |
301 | 永久转移 |
305 | 使用代理 |
307 | 临时重定向 |
400 | 错误的请求 |
402 | 需要抵消 |
404 | 未找到 |
408 | 请求超时 |
417 | 未实现预期 |
500 | 内部服务器错误 |
503 | 服务不可用 |
505 | http版本不受支持 |
在顶级信息下面有trace
日志,提供页面生命周期的细节。它提供自页面初始化以来经过的时间(秒)。如下图所示 -
下一个信息块是控制树,它以分层的方式列出页面上的所有控件:
最后在会话和应用程序状态摘要,cookie和标题集合之后列出所有服务器变量。
跟踪对象允许将自定义信息添加到跟踪输出。 它有两个方法来完成这个操作:write
方法和warn
方法。
更改page_load
事件处理程序以使用write
方法记录程序执行过程:
trace.write("页面已经开始加载...");
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
块保存任何可能产生错误或者不产生错误的代码,catch
块捕获错误。 程序运行时,会在跟踪日志中发送警告。
应用程序级别跟踪适用于网站中的所有页面。 它通过在web.config
文件中放入以下代码行来实现:
<system.web>
<trace enabled="true" />
</system.web>
虽然asp.net可以检测到所有的运行时错误,但仍然有一些细微的错误。 通过跟踪观察错误是为了方便开发人员发现程序问题,而不是为了用户。
因此,为了截获这种情况,可以在应用程序的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>
部分可能有的属性:
为了针对不同类型的错误放置不同的自定义错误页面,根据错误的状态代码使用<error>
子标记,其中指定了不同的错误页面。
要实现页面级错误处理,可以修改page
指令:
<%@ page language="c#" autoeventwireup="true" codebehind="default.aspx.cs"
inherits="errorhandling._default" trace ="true" errorpage="pageerror.html" %>
由于asp.net调试本身是一个重要的主题,因此在接下来的教程中,将在单独一篇文章讨论它。