错误页是在web.config里设置的,在web.config里设置页面出现何种错误而跳转到哪个指定的页面。这就叫做定制错误页
当页面发生错误的时候,asp.net会将错误信息展示出来。这样一来不好看,二来也会泄露网站的内部实现信息,给网站带来安全隐患,因此需要定制错误页,发生错误时显示开发人员定制的页面
web.config页
<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细消息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<!--定义友好的错误,出错默认导向到 ErrorPage.html -->
<!--mode属性:mode="On"的时候就表示对所以访问者都显示定制错误页;mode="Off"就表示关闭定制的错误页,页面错误的时候将本地和远程用户都会看到详细的错误信息;mode="RemoteOnly"就表示本地用户将看到详细错误信息(这里的本地的意思即:你直接登陆网站服务器上),而远程用户将会看到程序员定制的错误页。所以一般情况下我们都建议将mode属性设为RemoteOnly-->
<!--RedirectMode 属性:获取或设置一个值,该值指示在将用户重定向到自定义错误页面时,是否应更改请求的 URL。默认值为 ResponseRedirect。如果 RedirectMode 属性设置为 ResponseRedirect,则将用户重定向到该错误页面,并且原始 URL 更改为该错误页面的 URL。如果 RedirectMode 属性设置为 ResponseRewrite,则将用户定向到错误页面,并且不更改浏览器中的原始 URL。【ResponseRedirect其实就是Response.Redirect;而ResponseRewrite其实就是Server.Transfer】 -->
<!--defaultRedirect属性:应用程序在发生错误时重定向到的默认URL-->
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/ErrorPage.aspx">
<!--在它下面还可以分的很详细,可以针对一些错误码来指向我们针对这些错误码而定制的页面-->
<error statusCode="403" redirect="~/NoAccess.aspx" />
<error statusCode="404" redirect="~/FileNotFound.aspx" />
</customErrors>
</system.web>
</configuration>
可能出错的页面 演示错误页面.aspx.cs
因为在web.config配置文件里设置了“定制错误页”,所以这个页面一出错,就会跳转到程序员定制的错误页“ErrorPage.aspx”
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
namespace 错误页
{
public partial class 演示错误页面 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//假设我要访问数据库(这里的数据库连接字符串是错误的,运行这个页面的时候就会报错了)
//string connStr = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection("Ip=168.192.1.1@userName=sa;password=123456");
conn.Open();
}
}
}
程序员自己定制的错误页(即原始网页出错后要跳转到的定制页面)
ErrarPage.aspx页的源代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ErrarPage.aspx.cs" Inherits="错误页.ErrarPage" %>
<!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>
<p>程序发生错误了,抱歉!</p>
</div>
</form>
</body>
</html>
ErrarPage.aspx.cs 后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace 错误页
{
public partial class ErrarPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) //如果是第一次进入页面
{
//GetLastError()方法是:获取前一个异常,并将它返回
Exception ex = HttpContext.Current.Server.GetLastError().GetBaseException();
//将获得到异常信息写入到d:/errerinof.log 文件中
File.AppendAllText("d:/errerinof.log", "异常信息:"+ex.Message +"堆栈详细信息:"+ ex.StackTrace +"异常发生页:"+Request.Url.ToString()+ "错误源:"+ex.Source+"发生时间:"+DateTime.Now.ToString());
//或者可以在应用程序中添加一个文本文件,将错误信息写入都可以(添加->新建项->常规-文本文件)
//File.AppendAllText(Server.MapPath("Errer.txt"), ex.Message + ex.StackTrace + DateTime.Now.ToString());
//------------------------------------------------------------------------------
// Create意思是:在指定路径中创建或覆盖文件;Close:关闭当前流并释放与之关联的所有资源。
//File.Create("d:/errerinof1.log").Close();
//System.IO.FileStream fs = new System.IO.FileStream("", System.IO.FileMode.Append, System.IO.FileAccess.Write);
//------------------------------------------------------------------------------
//Exception objErr = Server.GetLastError().GetBaseException();
//string error = string.Empty;
//string errortime = string.Empty;
//string erroraddr = string.Empty;
//string errorinfo = string.Empty;
//string errorsource = string.Empty;
//string errortrace = string.Empty;
//error += "发生时间:" + System.DateTime.Now.ToString() + "<br>";
//errortime = "发生时间:" + System.DateTime.Now.ToString();
//error += "发生异常页: " + Request.Url.ToString() + "<br>";
//erroraddr = "发生异常页: " + Request.Url.ToString();
//error += "异常信息: " + objErr.Message + "<br>";
//errorinfo = "异常信息: " + objErr.Message;
error +="错误源:"+objErr.Source+"<br>";
error += "堆栈信息:" + objErr.StackTrace + "<br>";
//errorsource = "错误源:" + objErr.Source;
//errortrace = "堆栈信息:" + objErr.StackTrace;
//error += "--------------------------------------<br>";
//Server.ClearError();
//Application["error"] = error;
}
}
}
}
以上只演示了一个定制错误页 ErrorPage.aspx
针对一些错误码来指向我们针对这些错误码而定制的页面NoAccess.aspx,FileNotFound.aspx这两个定制错误页我就不一一演示了。实现原理同上。
另外一种方式是在Global.asax中的protected void Application_Error(object sender, EventArgs e)方法中设定
(其实就是在一用程序声明周期管道事件中设定)
protected void Application_Error(object sender, EventArgs e)
{
//错误信息 (注意:Environment.NewLine表示换行符)
string errorMsg = Context.Error.Message + Environment.NewLine + Context.Error.StackTrace + Environment.NewLine;
//将错误信息写入到日记
File.AppendAllText(Server.MapPath("~/log.txt"),errorMsg);
//跳转到错误页
Response.Redirect("~/ErrorPage.html");
}