日期:2014-05-17  浏览次数:21047 次

ASP.NET 错误页处理

ASP.NET 提供三种用于在出现错误时捕获和响应错误的主要方法:Page_Error 事件、Application_Error 事件以及应用程序配置文件 (Web.config)。

如果您不调用 Server.ClearError 或者捕获 Page_Error 或 Application_Error 事件中的错误,则将根据 Web.config 文件的 <customErrors> 部分中的设置处理错误。在 <customErrors> 部分,可将重定向页指定为默认的错误页 (defaultRedirect) 或者根据引发的 HTTP 错误代码指定特定页。您可使用此方法自定义用户收到的错误信息。
尽管您可在 <customErrors> 部分中 defaultRedirect 属性的值中引用默认的错误页,但是您还可根据引发的 HTTP 错误代码指定要重定向到的特定页。<error> 子元素允许使用此选项
<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>

asp.net自定义错误处理页面方法一

1、添加Web.config, < system.web>< /system.web>中添加< customErrors mode="On"

defaultRedirect="ApplicationErroy.aspx" >< /customErrors>节点,

2、添加错误处理页面:ApplicationErroy.aspx调用下面的方法:
private void DealErroy()?????
???? {?????
???????? HttpException erroy = new HttpException();?????
???????? string strCode = erroy.ErrorCode.ToString();?????
???????? string strMsg = erroy.Message;?????
???????? erroy.HelpLink = "sss";?????
???????? Response.Write("ErrorCode:" + strCode + "< br>");?????
???????? Response.Write("Message:" + strMsg + "< br>");?????
???????? Response.Write("HelpLink:" + erroy.HelpLink + "< br>");?????
???????? Response.Write("Source:" + erroy.Source + "< br>");?????
???????? Response.Write("TargetSite:" + erroy.TargetSite + "< br>");?????
???????? Response.Write("InnerException:" + erroy.InnerException + "< br>");?????
???????? Response.Write("StackTrace:" + erroy.StackTrace + "< br>");?????
???????? Response.Write("GetHtmlErrorMessage:" + erroy.GetHtmlErrorMessage() + "< br>");?????
???????? Response.Write("erroy.GetHttpCode().ToString():" + erroy.GetHttpCode().ToString() + "< br>");?????
???????? Response.Write("erroy.Data.ToString()::" + erroy.Data.ToString() + "< br>");?????
???? }????????
???
private void DealErroy()?
????? {?
????????? HttpException erroy = new HttpException();?
????????? string strCode = erroy.ErrorCode.ToString();?
????????? string strMsg = erroy.Message;?
????????? erroy.HelpLink = "sss";?
????????? Response.Write("ErrorCode:" + strCode + "< br>");?
????????? Response.Write("Message:" + strMsg + "< br>");?
????????? Response.Write("HelpLink:" + erroy.HelpLink + "< br>");?
????????? Response.Write("Source:" + erroy.Source + "< br>");?
????????? Response.Write("TargetSite:" + erroy.TargetSite + "< br>");?
????????? Response.Write("InnerException:" + erroy.InnerException + "< br>");?
????????? Response.Write("StackTrace:" + erroy.StackTrace + "< br>");?
????????? Response.Write("GetHtmlErrorMessage:" + erroy.GetHtmlErrorMessage() + "< br>");?
????????? Response.Write("erroy.GetHttpCode().ToString():" + erroy.GetHttpCode().ToString() + "< br>");?
????????? Response.Write("erroy.Data.ToString()::" + erroy.Data.ToString() + "< br>");?
????? }

这种方法不能完整地显示错误信息;