自定义错误页

Web.Config:

ExpandedBlockStart.gif 代码
< 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 >

 

 

Global.asax:

 

ExpandedBlockStart.gif 代码
<% @ 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 >

 

 

Error.txt:

ExpandedBlockStart.gif 代码
<! 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 >< href ="/" >< img  src ="logo.jpg"  alt ="xxxx网"   /></ a ></ h1 >
    
< ul >
        
< p > 没有找到资源, < href ="javascript:history.go(-1);" > 返回上一页 </ a > ;或 < href ="/" > 返回首页 </ a > </ p >
    
</ ul >
    
< ol  class ="footer" > &copy; 2009 www.xxx.com </ ol >
</ div >
</ body >
</ html >

 

 

Web.Config与Global.asax的区别:

Config可以根据不同的错误类型定义不同的错误页,网站重定义转向新的错误页面。

Global,在全局错误中写入应用程序事件错误信息,并在当前页输出自定义的错误内容

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值