一,创建错误页面error.aspx,前台代码如下(可根据实际需要增加元素):
.style1 { width: 74px; } .style2 { width: 79px; }
二,在全局处理文件中的 Application_Error 中加入错误处理的代码,如下:
// 在出现未处理的错误时运行的代码 Exception objErr = Server.GetLastError().GetBaseException(); string sError = "异常页面:" + HttpContext.Current.Request.Url.ToString() + "异常信息:" + objErr.Message + "处理方法:请刷新页面重试或联系系统管理员。"; //清除之前的异常 Server.ClearError(); //这里如果用Session["ProError"]会出错,所以用 Application["AppError"] Application["AppError"] = sError; //这里不能用Response.Redirect System.Web.HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath + "/error.aspx");
三,在错误页面初始化的时候, 输出错误信息,如下:
if (!IsPostBack) { if (Application["AppError"] != null) { try { string msg = Application["AppError"].ToString(); msg = msg.Replace("\"", ""); lblMessage1.Text = msg.Substring(0, msg.IndexOf("异常信息")); lblMessage2.Text = msg.Substring(msg.IndexOf("异常信息"), msg.IndexOf("处理方法") - msg.IndexOf("异常信息")); lblMessage3.Text = msg.Substring(msg.LastIndexOf("处理方法")); } catch (Exception ex) { lblMessage1.Text = ex.Message; } } }
以上步骤即可完成错误页面的配置,除此之外,还可通过Web.Config配置错误页面,预览效果如下:
查看更多关于Web项目中创建简单的错误处理页面_html/css_WEB-ITnose的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did107058