欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

.net三步配置错误页面,让你的网站远离不和谐的页面_html/css_WEB-ITnose

程序员文章站 2024-02-07 23:15:52
...
如果你的网站出现一堆让人看不懂的报错,那么你就不是一个合格的程序员,也不是一个合格的站长。

下面的方面可以帮助你的网站远离让人头大的页面。

第一步:配置web.config

打开web.config,在节点下添加如下代码:




第二步:建立错误页面

在网站根目录下建立404页面(找不到页面):404.html

403页面(服务器禁止访问):403.html

这样就可以解决一部分问题,但是如果我们程序有些bug,碰巧被用户发现,那么还是会返回给用户一个不友好的报错页面。所以我们还要建立一个ErrorPages.aspx捕捉那些我们不知道的错误页面,用以处理那些报错,显示给用户良好的页面。

第三步:捕捉未知错误,显示友好提示信息。

在ErrorPages.aspx.cs中加入以下代码:

[c-sharp] view plain copy

  1. if (!IsPostBack)
  2. {
  3. HttpException erroy = new HttpException();
  4. string strCode = erroy.ErrorCode.ToString();
  5. string strMsg = erroy.Message;
  6. StringBuilder sb = new StringBuilder();
  7. sb.Append("-----------记录开始时间:" + System.DateTime.Now+"-----------------
    ");
  8. erroy.HelpLink = Request.QueryString["aspxerrorpath"];
  9. sb.Append("ErrorCode:" + strCode + "
    ");
  10. sb.Append("Message:" + strMsg + "
    ");
  11. sb.Append("HelpLink:" + erroy.HelpLink + "
    ");
  12. sb.Append("Source:" + erroy.Source + "
    ");
  13. sb.Append("TargetSite:" + erroy.TargetSite + "
    ");
  14. sb.Append("InnerException:" + erroy.InnerException + "
    ");
  15. sb.Append("StackTrace:" + erroy.StackTrace + "
    ");
  16. sb.Append("GetHtmlErrorMessage:" + erroy.GetHtmlErrorMessage() + "
    ");
  17. sb.Append("erroy.GetHttpCode().ToString():" + erroy.GetHttpCode().ToString() + "
    ");
  18. sb.Append("erroy.Data.ToString():" + erroy.Data.ToString() + "
    ");
  19. sb.Append("----------记录结束----------------");
  20. Response.Write(sb.ToString());
  21. }

到此为止:网站错误配置完成。当然错误处理页面你可以随意定义,你可以把捕捉到的错误写入数据库或者文件,只显示一些提示信息给用户,你也可以把错误信息处理后友好的显示给用户。

还有一种方法是在Global.asax中的void Application_Error(object sender, EventArgs e)方法中定义;现给以大体方法,具体操作可以根据实际情况给以修改。

在Global.asax文件中修改:

void Application_Error(object sender, EventArgs e)
{
//在出现未处理的错误时运行的代码
Exception erroy = Server.GetLastError();
string err = "出错页面是:" + Request.Url.ToString() + "";
err += "异常信息:" + erroy.Message + "";
err += "Source:" + erroy.Source + "";
err += "StackTrace:" + erroy.StackTrace + "";
//清除前一个异常
//Server.ClearError();
//此处理用Session["ProError"]出错。所以用 Application["ProError"]
Application["erroy"] = err;
//此处不是page中,不能用Response.Redirect("../ErrorPages.aspx");
System.Web.HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath + "/ErrorPages.aspx");

}

在ErrorPages.aspx.cs文件中修改

protected void Page_Load(object sender, EventArgs e)
{
//显示程序中的错误码

if (!IsPostBack)
{

//显示程序中的错误码

if (Application["erroy"] != null)
{

Response.Write(Application["erroy"].ToString());

}

}

}

补充:使用上述方法实现的跳转,返回的HTTP状态码全部是302,本来应该返回404的也给返回302.这样对搜索引擎优化很不利。所以我们应该在Global.asax文件中添加如下代码:

[c-sharp] view plain copy

  1. protected void Application_Error(Object sender, EventArgs e)
  2. {
  3. System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/Web.config");
  4. System.Web.Configuration.CustomErrorsSection customErrors = (System.Web.Configuration.CustomErrorsSection)config.GetSection("system.web/customErrors");
  5. if (customErrors != null && (customErrors.Mode == System.Web.Configuration.CustomErrorsMode.On || customErrors.Mode == System.Web.Configuration.CustomErrorsMode.RemoteOnly))
  6. {
  7. System.Web.HttpApplication app = (HttpApplication)sender;
  8. System.Exception lastError = app.Server.GetLastError();
  9. System.Web.HttpException httpEx = (HttpException)lastError;
  10. if (httpEx != null)
  11. {
  12. int httpErrorCode = httpEx.GetHttpCode();
  13. string redirect = customErrors.DefaultRedirect;
  14. foreach (System.Web.Configuration.CustomError error in customErrors.Errors)
  15. {
  16. if (error.StatusCode == httpErrorCode) redirect = error.Redirect;
  17. }
  18. app.Server.ClearError();
  19. app.Context.Response.StatusCode = httpErrorCode;
  20. Server.Transfer(redirect);
  21. }
  22. }
  23. }

这样问题就得以解决了。