Web.Config:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<
customErrors mode
=
"
On
"
defaultRedirect
=
"
GeneralError.aspx
"
>
< error statusCode = " 404 " redirect = " GeneralError.aspx " />
< error statusCode = " 403 " redirect = " GeneralError.aspx " />
< error statusCode = " 500 " redirect = " GeneralError.aspx " />
</ customErrors >
< error statusCode = " 404 " redirect = " GeneralError.aspx " />
< error statusCode = " 403 " redirect = " GeneralError.aspx " />
< error statusCode = " 500 " redirect = " GeneralError.aspx " />
</ customErrors >
Global.asax:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<%
@ Application Language
=
"
C#
"
%>
<% @Import Namespace = System.IO %>
< script runat = " server " >
void Application_Start( object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
}
void Application_End( object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码
}
void Application_Error( object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
Exception objErr = Server.GetLastError().GetBaseException(); // 获取错误
string err = " Error Caught in Application_Error event\n " +
" Error in: " + Request.Url.ToString() +
" \nError Message: " + objErr.Message.ToString() +
" \nStack Trace: " + objErr.StackTrace.ToString();
// 将捕获的错误写入windows的应用程序日志中,可从事件查看器中访问应用程序日志。
System.Diagnostics.EventLog.WriteEntry( " Test2 " , err, System.Diagnostics.EventLogEntryType.Error);
Server.ClearError(); // 清除异常,其他地方不再捕获此异常。
string virtualPath = " Error.txt " ;
StreamReader sr = null ;
try
{
sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(virtualPath), Encoding.GetEncoding( " gb2312 " ));
}
catch
{
sr = new StreamReader(virtualPath);
}
string strOut = sr.ReadToEnd();
sr.Close();
Response.Write(strOut);
Response.End();
}
void Session_Start( object sender, EventArgs e)
{
// 在新会话启动时运行的代码
}
void Session_End( object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。
}
</ script >
<% @Import Namespace = System.IO %>
< script runat = " server " >
void Application_Start( object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
}
void Application_End( object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码
}
void Application_Error( object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
Exception objErr = Server.GetLastError().GetBaseException(); // 获取错误
string err = " Error Caught in Application_Error event\n " +
" Error in: " + Request.Url.ToString() +
" \nError Message: " + objErr.Message.ToString() +
" \nStack Trace: " + objErr.StackTrace.ToString();
// 将捕获的错误写入windows的应用程序日志中,可从事件查看器中访问应用程序日志。
System.Diagnostics.EventLog.WriteEntry( " Test2 " , err, System.Diagnostics.EventLogEntryType.Error);
Server.ClearError(); // 清除异常,其他地方不再捕获此异常。
string virtualPath = " Error.txt " ;
StreamReader sr = null ;
try
{
sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(virtualPath), Encoding.GetEncoding( " gb2312 " ));
}
catch
{
sr = new StreamReader(virtualPath);
}
string strOut = sr.ReadToEnd();
sr.Close();
Response.Write(strOut);
Response.End();
}
void Session_Start( object sender, EventArgs e)
{
// 在新会话启动时运行的代码
}
void Session_End( object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。
}
</ script >
Error.txt:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<!
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 >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
< title > 没有找到您要的资源 - xxxx网 </ title >
< meta http-equiv ="X-UA-Compatible" content ="IE=EmulateIE7" />
< style rel ="stylesheet" type ="text/css" >
body { padding : 0px ; margin : 0px ; text-align : left ; font : 12px Tahoma,Arial,sans-serif ; line-height : 150% ; color : #000 ; background : #D7F5FE ; }
h1, ul,p,ol { margin : 0 ; padding : 0 }
img { border : 0 ; }
a:link, a:visited { text-decoration : underline ; color : #f20 ; }
a:hover { text-decoration : underline ; color : #00f ; }
.error { margin : 30px auto ; width : 600px ; height : 450px ; background : #fff url('notfoundbg.jpg') no-repeat top center ; }
.error h1 { margin : 30px 0 ; text-align : center ; }
.error ul { margin : 150px auto 0 auto ; text-align : center ; }
.error ol { margin : 100px auto 0 auto ; text-align : center ; color : #919191 ; }
</ style >
</ head >
< body >
< div class ="error" >
< h1 >< a href ="/" >< img src ="logo.jpg" alt ="xxxx网" /></ a ></ h1 >
< ul >
< p > 没有找到资源, < a href ="javascript:history.go(-1);" > 返回上一页 </ a > ;或 < a href ="/" > 返回首页 </ a > 。 </ p >
</ ul >
< ol class ="footer" > © 2009 www.xxx.com </ ol >
</ div >
</ body >
</ html >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
< title > 没有找到您要的资源 - xxxx网 </ title >
< meta http-equiv ="X-UA-Compatible" content ="IE=EmulateIE7" />
< style rel ="stylesheet" type ="text/css" >
body { padding : 0px ; margin : 0px ; text-align : left ; font : 12px Tahoma,Arial,sans-serif ; line-height : 150% ; color : #000 ; background : #D7F5FE ; }
h1, ul,p,ol { margin : 0 ; padding : 0 }
img { border : 0 ; }
a:link, a:visited { text-decoration : underline ; color : #f20 ; }
a:hover { text-decoration : underline ; color : #00f ; }
.error { margin : 30px auto ; width : 600px ; height : 450px ; background : #fff url('notfoundbg.jpg') no-repeat top center ; }
.error h1 { margin : 30px 0 ; text-align : center ; }
.error ul { margin : 150px auto 0 auto ; text-align : center ; }
.error ol { margin : 100px auto 0 auto ; text-align : center ; color : #919191 ; }
</ style >
</ head >
< body >
< div class ="error" >
< h1 >< a href ="/" >< img src ="logo.jpg" alt ="xxxx网" /></ a ></ h1 >
< ul >
< p > 没有找到资源, < a href ="javascript:history.go(-1);" > 返回上一页 </ a > ;或 < a href ="/" > 返回首页 </ a > 。 </ p >
</ ul >
< ol class ="footer" > © 2009 www.xxx.com </ ol >
</ div >
</ body >
</ html >
Web.Config与Global.asax的区别:
Config可以根据不同的错误类型定义不同的错误页,网站重定义转向新的错误页面。
Global,在全局错误中写入应用程序事件错误信息,并在当前页输出自定义的错误内容