谁沉冥到,那无边的深,将热爱着,这最生动的生

.net 2.0 c#学习

导航

C#页面统一错误处理

第一种:

web.config里......

 <compilation
         defaultLanguage="c#"
         debug="false"
    />

    <customErrors   mode="RemoteOnly" defaultRedirect="error/genericerror.htm" >
      <error statusCode="500" redirect="error/callsupport.htm"/>
      <error statusCode="404" redirect="error/notfound.htm"/>
      <error statusCode="403" redirect="error/noaccess.htm"/>
    </customErrors>  

建立四个htm(aspx也行)文件,放到error文件夹下面.

error/genericerror.htm   一般性错误

error/callsupport.htm  内部服务器错误

error/notfound.htm  网页未找到

error/noaccess.htm  网页已禁止

第二种:这个是别人写的,我也是查到的.呵...

在global里面控制的.然后把错误信息插入一个表中.再将程序导入到一个错误页面.还可以专门做一个错误显示页.
在Application_Error方法中控制.

protected void Application_Error(Object sender, EventArgs e)
{
Exception Error = Server.GetLastError();
if (Error != null)
{

SqlServer ss = new SqlServer();
string sql = "insert into exception (errorPage,errorLocation,errorReason,errorOuter,errorStackTrace)values";//表中还有出错时间
sql=sql+"('"+ Request.Url.ToString() +"',";
sql=sql+"'"+ Error.InnerException.TargetSite +"',";
sql=sql+"'"+ Error.InnerException.Message +"',";
sql=sql+"'"+ Error.InnerException.ToString() +"',";
sql=sql+"'"+ Error.InnerException.StackTrace +"')";
try
{
ss.execNoReturn(sql);
}
catch(Exception ex)
{
throw ex;
}
finally
{
Server.Transfer(this.Request.ApplicationPath + "\\Error.aspx");
}
}
}

方法都有些简单,适合小一点的项目.

posted on 2006-09-21 09:22  天若有情  阅读(1077)  评论(1编辑  收藏  举报