当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  asp.net定制简单的错误处理页面

 asp.net定制简单的错误处理页面

点击次数:23 次 发布日期:2008-11-26 11:34:02 作者:源代码网
源代码网推荐      通常web应用程序在发布后,为了给用户一个友好界面和使用体验,都会在错误发生时跳转至一个自定义的错误页面,而不是asp.net向用户暴露出来的详细的异常列表。
源代码网推荐  简单的错误处理页面可以通过web.config来设置
源代码网推荐   <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
源代码网推荐   <error statusCode="403" redirect="NoAccess.htm" />
源代码网推荐   <error statusCode="404" redirect="FileNotFound.htm" />
源代码网推荐   </customErrors>
源代码网推荐  如果想通过编程的方式来呈现错误原因,可以通过Page_Error事件来做这件事.
源代码网推荐  另一种方式则可以通过Global.asax来实现,我觉得这种方式较为方便,另外如果能结合一个单独的更加友好的页面,则看来起更舒服一些
源代码网推荐  
源代码网推荐  
源代码网推荐  Global.asax(如果需要,可以记录错误日志) void Application_Error(object sender, EventArgs e)
源代码网推荐   {
源代码网推荐   Exception objErr = Server.GetLastError().GetBaseException();
源代码网推荐   string error = "发生异常页: " + Request.Url.ToString() + "<br>";
源代码网推荐   error += "异常信息: " + objErr.Message + "<br>";
源代码网推荐   Server.ClearError();
源代码网推荐   Application["error"] = error;
源代码网推荐   Response.Redirect("~/ErrorPage/ErrorPage.aspx");
源代码网推荐   }
源代码网推荐  ErrorPage.aspx
源代码网推荐  
源代码网推荐   protected void Page_Load(object sender, EventArgs e)
源代码网推荐   {
源代码网推荐   ErrorMessageLabel.Text = Application["error"].ToString();
源代码网推荐   }当最终用户使用应用程序的时候,他们可能不想知道错误的原因,这个时候,我们可以通过复选框来实现,是否呈现错误的原因。可将Label放在一个div中,然后用复选框来决定是否呈现div
源代码网推荐   <script language="javascript" type="text/javascript">
源代码网推荐  <!--
源代码网推荐  
源代码网推荐  function CheckError_onclick() {
源代码网推荐   var chk = document.getElementById("CheckError");
源代码网推荐   var divError = document.getElementById("errorMsg");
源代码网推荐   if(chk.checked)
源代码网推荐   {
源代码网推荐   divError.style.display = "inline";
源代码网推荐   }
源代码网推荐   else
源代码网推荐   {
源代码网推荐   divError.style.display = "none";
源代码网推荐   }
源代码网推荐  }
源代码网推荐  
源代码网推荐  // -->
源代码网推荐   </script>
源代码网推荐  
源代码网推荐  http://www.cnblogs.com/EasyLive2006/archive/2007/01/07/613922.html
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华