示例:html
1.创建两个页面,一个为访问特定文件时找不到文件的出错页FileNotFound.htm,另外一个为其它出错时的错误页:defaultErr.aspx。web
2.创建ErrorTest.aspx,当运行此页时,系统会出错浏览器
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ErrorTest.aspx.cs" Inherits="企业网站.ErrorTest" %> <!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></title> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html>
在它的page_load事件中的代码:安全
if (!IsPostBack) { SqlConnection conn = new SqlConnection("IP:127.0.0.1&db=tt"); conn.Open(); }
3.设置web.config文件服务器
<?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细消息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/ErrorPages/defaultErr.aspx"> <error statusCode="404" redirect="~/ErrorPages/FileNotFound.htm" /> </customErrors> <compilation debug="true" targetFramework="4.0" /> <httpRuntime requestValidationMode="2.0"/> </system.web> </configuration>
4.当产生错误时,有个日记用来记录错误的具体信息,在项目中创建ErrorRecords.txt文件网站
在defaultErr.aspx页的Page_Load事件中写入如下代码:spa
if (!IsPostBack) { Exception ex= HttpContext.Current.Server.GetLastError(); File.AppendAllText(Server.MapPath("~/ErrorRecords.txt"), ex.StackTrace); }
5.运行ErrorTest.aspx,会自动呈现defaultErr.aspx页面,并会把出错的具体信息写到ErrorRecords.txt中。.net