Asp.Net :自定义错误页(Web.Config与Global.asax的不同用法。。。。。)
Posted on 2010-01-11 17:47 且行且思 阅读(1977) 评论(0) 编辑 收藏 举报
Web.Config:

<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:

<%@ 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:

<!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,在全局错误中写入应用程序事件错误信息,并在当前页输出自定义的错误内容。。。。
分类:
Asp.Net
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
2007-01-11 正则抓取SINA天气预报数据!!!
2007-01-11 抓取国家气象局天气预报3天!!