鲨丁鱼.net技术小栈

这里讨论.net的web和form开发,还有其它关于WEB开发和安全的全部知识点,顺带一些经典的有意思的杂文!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

自定义错误页面

Posted on 2005-12-16 11:07  King0502  阅读(251)  评论(0编辑  收藏  举报
2.建立一个页面类(假设叫BasePage),继承于Page,然后你的站点的所有页面都从这个类继承,这样你只要在BasePage类里:override OnInit事件,在里面:
this.Error += new System.EventHandler(this.error());
然后在error()方法里做你的统一的错误处理,这样,所有页的错误处理就一样了。

?
给你点例子:
protected void error(object sender, System.EventArgs e)
{
string errMsg;
Exception currentError = Server.GetLastError();
errMsg = "<h1>Page Error</h1><hr/>An unexpected error has occurred on this page. The system " +"administrators have been notified. Please feel free to contact us with the information " +"surrounding this error.<br/>"+
				"The error occurred in: "+Request.Url.ToString()+"<br/>"+
				"Error Message: <font class=\"ErrorMessage\">"+ currentError.Message.ToString() + "</font><hr/>"+
				"<b>Stack Trace:</b><br/>"+
				currentError.ToString();

			if ( !(currentError is AppException) )
			{
				// It is not one of ours, so we cannot guarantee that it has been logged
				// into the event log.
				LogEvent( currentError.ToString(), EventLogEntryType.Error );
			}

			Response.Write( errMsg );
			Server.ClearError();
		}