要实现将所有的错误信息在一个页面中显示,首先用户必须将Web.config中的 <customErrors mode="Off"/>
设置为Off。所有的页面都从一个基础页面继承。基础页面定义如下:
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
![]()
using Human.SystemFrameworks;
using Human.Common;
using Human.Common.Data;
![]()
namespace Human.WebUI
{
/// <summary>
/// PageBase 的摘要说明。
/// </summary>
public class PageBase:System.Web.UI.Page
{
private const String KEY_CACHEACCOUNT = "Cache:Account:";
private string _ErrorMessage;
![]()
private static string UrlSuffix
{
get
{
return HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath;
}
}
public static String SecureUrlBase
{
get
{
return (HumanConfiguration.EnableSSL ? @"https://": @"http://") + UrlSuffix;
}
}
![]()
public PageBase()
{
Page.Error+=new EventHandler(Page_Error);
}
![]()
protected void Page_Error(object Sender, EventArgs e)
{
Exception objErr = Server.GetLastError().GetBaseException();
ErrorMessage=objErr.Message;
}
![]()
public string ErrorMessage
{
get
{
return _ErrorMessage;
}
set
{
_ErrorMessage=value;
}
}
![]()
public static string UrlBase
{
get
{
return @"http://"+UrlSuffix;
}
}
![]()
public DataSet Account
{
get
{
try
{
return (DataSet)(Session[KEY_CACHEACCOUNT]);
}
catch
{
return (null); // for design time
}
}
set
{
if ( null == value )
{
Session.Remove(KEY_CACHEACCOUNT);
}
else
{
Session[KEY_CACHEACCOUNT] = value;
}
}
}
![]()
}
}
新建一个ErrorPage.aspx页面,在页面中增加一个Label控件,用于显示错误信息!
定义如下:
try
{
HttpException checkexception=(HttpException)appException;
errorcode=checkexception.GetHttpCode();
}
catch
{
}
但这样也是可行的。
最后需要增加一个关键的语句,即在Global.aspx中的Application_Error事件中加入:Server.Transfer("ErrorPage.aspx");即可。
设置为Off。所有的页面都从一个基础页面继承。基础页面定义如下:
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using Human.SystemFrameworks;
using Human.Common;
using Human.Common.Data;
namespace Human.WebUI
{
/// <summary>
/// PageBase 的摘要说明。
/// </summary>
public class PageBase:System.Web.UI.Page
{
private const String KEY_CACHEACCOUNT = "Cache:Account:";
private string _ErrorMessage;
private static string UrlSuffix
{
get
{
return HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath;
}
}
public static String SecureUrlBase
{
get
{
return (HumanConfiguration.EnableSSL ? @"https://": @"http://") + UrlSuffix;
}
}
public PageBase()
{
Page.Error+=new EventHandler(Page_Error);
}
protected void Page_Error(object Sender, EventArgs e)
{
Exception objErr = Server.GetLastError().GetBaseException();
ErrorMessage=objErr.Message;
}
public string ErrorMessage
{
get
{
return _ErrorMessage;
}
set
{
_ErrorMessage=value;
}
}
public static string UrlBase
{
get
{
return @"http://"+UrlSuffix;
}
}
public DataSet Account
{
get
{
try
{
return (DataSet)(Session[KEY_CACHEACCOUNT]);
}
catch
{
return (null); // for design time
}
}
set
{
if ( null == value )
{
Session.Remove(KEY_CACHEACCOUNT);
}
else
{
Session[KEY_CACHEACCOUNT] = value;
}
}
}
}
}
定义如下:
1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Web;
7
using System.Web.SessionState;
8
using System.Web.UI;
9
using System.Web.UI.WebControls;
10
using System.Web.UI.HtmlControls;
11![]()
12
namespace Human.WebUI
13
{
14
/// <summary>
15
/// ErrorPage 的摘要说明。
16
/// </summary>
17
public class ErrorPage : PageBase
18
{
19
protected System.Web.UI.WebControls.Label ErrorMessageLabel;
20
21
private PageBase page;
22![]()
23
private void Page_Load(object sender, System.EventArgs e)
24
{
25
if (!Page.IsPostBack)
26
{
27
page=(PageBase)Context.Handler;
28
string errMessage="";
29
System.Exception appException=Server.GetLastError();
30![]()
31
int errorcode=0;
32
try
33
{
34
HttpException checkexception=(HttpException)appException;
35
errorcode=checkexception.GetHttpCode();
36
}
37
catch
38
{
39
}
40
switch (errorcode)
41
{
42
case 404:
43
{
44
ErrorMessageLabel.Text="您访问的网页不存在!";
45
break;
46
}
47
case 403:
48
{
49
break;
50
}
51
default:
52
{
53
ErrorMessageLabel.Text=page.ErrorMessage;
54
ErrorMessage=null;
55
break;
56
}
57
}
58
}
59
}
60![]()
61
Web 窗体设计器生成的代码
81
}
82
}
83![]()
在上面的代码中,我不知道如何判断appException是不是HttpException类型,所以只有采用
using System;2
using System.Collections;3
using System.ComponentModel;4
using System.Data;5
using System.Drawing;6
using System.Web;7
using System.Web.SessionState;8
using System.Web.UI;9
using System.Web.UI.WebControls;10
using System.Web.UI.HtmlControls;11

12
namespace Human.WebUI13
{14
/// <summary>15
/// ErrorPage 的摘要说明。16
/// </summary>17
public class ErrorPage : PageBase18
{19
protected System.Web.UI.WebControls.Label ErrorMessageLabel;20
21
private PageBase page;22

23
private void Page_Load(object sender, System.EventArgs e) 24
{25
if (!Page.IsPostBack)26
{27
page=(PageBase)Context.Handler;28
string errMessage="";29
System.Exception appException=Server.GetLastError();30

31
int errorcode=0;32
try33
{34
HttpException checkexception=(HttpException)appException;35
errorcode=checkexception.GetHttpCode();36
}37
catch38
{39
}40
switch (errorcode)41
{42
case 404:43
{44
ErrorMessageLabel.Text="您访问的网页不存在!";45
break;46
}47
case 403:48
{49
break;50
}51
default:52
{53
ErrorMessageLabel.Text=page.ErrorMessage;54
ErrorMessage=null;55
break;56
}57
}58
}59
}60

61
Web 窗体设计器生成的代码81
}82
}83

try
{
HttpException checkexception=(HttpException)appException;
errorcode=checkexception.GetHttpCode();
}
catch
{
}
但这样也是可行的。
最后需要增加一个关键的语句,即在Global.aspx中的Application_Error事件中加入:Server.Transfer("ErrorPage.aspx");即可。


浙公网安备 33010602011771号