您的位置:首页 > 编程语言 > ASP

asp.net自定义错误页面

2010-01-21 10:07 567 查看
web.config

<customErrors mode="On" defaultRedirect="ApplicationErroy.aspx" >
<error statusCode="403" redirect="403.htm"/>
<error statusCode="404" redirect="404.htm"/>
<error statusCode="500" redirect="500.htm"/>
</customErrors>


Global.asax

<%@ Application Language="C#" %>
<mce: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 erroy = Server.GetLastError();
string err = "出错页面是:" + Request.Url.ToString() + "</br>";
err += "异常信息:" + erroy.Message + "</br>";
err += "Source:" + erroy.Source + "</br>";
err += "StackTrace:" + erroy.StackTrace + "</br>";
//清除前一个异常
Server.ClearError();

//此处理用Session["ProError"]出错。所以用 Application["ProError"]
Application["erroy"] = err;
//此处不是page中,不能用Response.Redirect("../frmSysError.aspx");
System.Web.HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath + "/ApplicationErroy.aspx");
}
void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码
}
void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。
}

// --></mce:script>


ApplicationErroy.aspx

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class ApplicationErroy : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//显示程序中的错误码
if (!IsPostBack)
{
//显示程序中的错误码
if (Application["erroy"] != null)
{
Response.Write(Application["erroy"].ToString());
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: