当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET的四种错误机制

ASP.NET
web中缓存的使用
浅谈.net 中的职责链模式的使用
c# 连接字符串数据库服务器端口号
虚拟主机上用Asp.net实现Urlrewrite
ADO.NET Entity Framework存取数据库中图片
.Net基础:.Net开发人员必知的八个网站
.Net Micro Framework中的线程
详解.NET编程过程中的线程冲突
微软 ASP.NET 内置安全架构的完全解析
运行 ASP 时脚本超时问题最终解决办法
.Net应用:制作ASP脚本组件实现重启服务器
.NET 环境下使用C# 防止SQL注入式攻击
利用C#远程存取Access数据库
.Net中图片的快速处理
浅谈对程序开发中异常的理解和认识
C#中的Adapter设计模式浅析
C# 2010命名和可选参数的新特性
.Net基础:学习反射中的动态创建对象
微软 ASP.NET 环境下的页面验证控件
如何在 C# 中发起会议之类的特殊邮件

ASP.NET的四种错误机制


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 147 ::
收藏到网摘: n/a

  摘自CNblogs Fantasy Soft的文章:

  1,Webconfig设置

  <?xml version="1.0"?>
    <configuration>
  <system.web>
  <customErrors mode="On" defaultRedirect="GenericErrorPage.htm">
  <error statusCode="403" redirect="Error403.htm" />
  <error statusCode="404" redirect="Error404.htm" /> 
  </customErrors>
  </system.web>
  </configuration>

  2,Global.asax设置

  protected void Application_Error(object sender, EventArgs e) {
  Exception objErr = Server.GetLastError().GetBaseException();
  Response.Write("Error:" + objErr.Message);
  Server.ClearError();
  }

  3,使用ErrorPage属性

  <script language="C#" runat="server">
  protected void Page_Load(object sender, EventArgs e) {
        this.ErrorPage = "ErrorPage.htm";
        }  
  </script>

  4,使用Page_Error事件处理方法
  protected void Page_Error(object sender, EventArgs e) {
  Exception objErr = Server.GetLastError().GetBaseException();
  Response.Write("Error:" + objErr.Message);
  Server.ClearError(); //同样要注意这句代码的使用
  } 

  根据优先级从高到低排序:Page_Error事件处理方法 > ErrorPage属性 > Application_Error事件处理方法 >  <customErrors>配置项。