asp.net指定页处理所有异常的几种方法

 

 前些天我在做论坛的时候想通过指定页处理所有异常, 刚开始只是在 web.config
中设置 <customErrors defaultRedirect="Err.aspx"   mode="RemoteOnly" /> 然后在
Err.aspx 中捕获,可始终没得到想要的异常信息。后来得知系统每离开处理页时都会清理所有未处理的异常。这里我提供三种方法:

方法一:
在 globe.aspx的Application_Error事件中定义一个 Server.Transfer("Err.aspx", false)。
然后在 Err.aspx 页中捕获,因为每发生异常 Application_Error事件都会被处发,
而 Server.Transfer的作用是在服务端转向另一页进行处理, 其中的 flase 参数
将阻止发生异常页执行清理工作。

部份代码如下:

1 None.gif protected   void  Application_Error(Object sender, EventArgs e) 
2 ExpandedBlockStart.gifContractedBlock.gif dot.gif
3InBlock.gif    if (Context != null && Context.IsCustomErrorEnabled) 
4InBlock.gif    Server.Transfer("Err.aspx", false); 
5ExpandedBlockEnd.gif}
 
6 None.gif

 

 1 None.gif StringBuilder sbErrorMsgs  =   new  StringBuilder();
 2 None.gifException ex  =  Server.GetLastError().GetBaseException();
 3 None.gif
 4 None.gif while  ( null   !=  ex)
 5 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 6InBlock.gif   if (ex is FileNotFoundException)
 7ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
 8InBlock.gif       sbErrorMsgs.Append("<p>找不到你请求的资源。</p>");
 9ExpandedSubBlockEnd.gif   }

10InBlock.gif   else
11ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
12InBlock.gif       sbErrorMsgs.AppendFormat("<p><b>{0}</b><br>{1}</p>", ex.GetType().Name, ex.Message);
13ExpandedSubBlockEnd.gif   }

14InBlock.gif   ex = ex.InnerException;
15ExpandedBlockEnd.gif}

16 None.gif
17 None.gifServer.ClearError();
18 None.gif
19 None.gifResponse.Write( sbErrorMsgs.ToString() );


方法二
在设置一个全局变量或Session ,在globe.aspx的Application_Error事件中给全局变量赋值,在然后在Err.aspx (错误处理页) 读取该变量内的信息进行处理。这时你需要在web.config 中设置 <customErrors mode="RemoteOnly" defaultRedirect="Err.aspx" />
以便发生异常时转向错误处理页。

方法三
要是你愿意也可以 IHttpHandler 来处理这些信息,和前向介绍的方法差不多。
具体步骤:
        1. 新建个错误处理页,里面可以可以是空的,因为根本用不着。这里我就用个文本吧如 err.txt 。
        2. 为了做测试,新建个index.aspx 页。
        3. 设置一个全局变量至于放在那,随便你了,为了方便举例我放到 index.aspx
页里,如:public static Exception expMsgs = null;
        4. 新建一个类来实现 IHttpHandler 接口,类名为MyIHttpHandler,所属命名空间 Test 。
        5. 在web.config中设置  <httpHandlers>  
<add verb="*" path="err.txt" type="Test.MyHttpHandler, Test" /> </httpHandlers>

部份代码如下:

 

 1 None.gif //  dot.gif dot.gif
 2 None.gif
 3 None.gif public   static  Exception expMsgs  =   null ;
 4 None.gif
 5 None.gif private   void  Page_Load( object  sender, System.EventArgs e)
 6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 7InBlock.gif    int i = Convert.ToInt32("a");
 8ExpandedBlockEnd.gif}

 9 None.gif
10 None.gif //  dot.gif dot.gif



 

 1 None.gif public   class  MyHttpHandler : IHttpHandler
 2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 3InBlock.gif        public void ProcessRequest(HttpContext context)
 4ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 5ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//* 在这里定义你的错误处理过程,
 6ExpandedSubBlockEnd.gif             * 处理后直接输出到客户端 */

 7InBlock.gif            HttpResponse Response = context.Response;            
 8InBlock.gif            Response.Write(idnex.expMsgs.ToString());
 9ExpandedSubBlockEnd.gif        }

10InBlock.gif
11InBlock.gif        public bool IsReusable
12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
13ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn true; }
14ExpandedSubBlockEnd.gif        }

15ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/qinfei/archive/2005/11/02/266758.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值