首页 新闻 赞助 找找看

ErrorPage无法捕获Exception

0
悬赏园豆:15 [已解决问题] 解决于 2008-10-04 20:03

普通頁面:

protected void Page_Load(object sender, EventArgs e)
    {
        throw new Exception("hello");
    }

ErrorPage:

 protected void Page_Error(object sender, EventArgs e)
    {
        Response.Write(Server.GetLastError().GetBaseException().Message);
        Server.ClearError();
    }

Web.Config

<customErrors mode="On" defaultRedirect="ErrorPage.aspx"/>


 問題:

ErrorPage.aspx的Server.GetLastError()总是为空,如何才能获取出错页面的Exception Message呢?

KymoWang的主页 KymoWang | 初学一级 | 园豆:160
提问于:2008-09-29 17:28
< >
分享
最佳答案
1

Global.asax文件中:

 protected void Application_Error(object sender, EventArgs e)
        {

            Exception ex = Server.GetLastError().GetBaseException();
            string errorurl = Request.Url.ToString();

            string errorInfo = "發生異常的頁面:"+errorurl ;
            errorInfo += "\r\n異常信息是:" + ex.Message.ToString();

            //寫入log文件
            ErrorLog log = new ErrorLog();
            log.WriteLog(errorInfo);

            errorInfo  = "發生異常的頁面:" + errorurl;
            errorInfo += "<br><br>異常信息是:" + ex.Message.ToString();

            Application["weberror"] = errorInfo;
            Server.ClearError();

            this.Response.Redirect("~/help/Error.aspx");

Error.aspx.cs文件中:

if (!IsPostBack)
            {
                if (Application["weberror"] != null)
                {
                    locErrorInfo.Text = Application["weberror"].ToString();
                    Application["weberror"] = null;
                }

            }

Steven Xiao | 菜鸟二级 |园豆:245 | 2008-09-30 10:01
其他回答(2)
0

throw new ArgumentOutOfRangeException(...);

重典 | 园豆:2442 (老鸟四级) | 2008-09-29 18:26
0

试试用 Application_Error() 取代 Page_Error()

参见下面帖子:

http://bytes.com/forum/thread286487.html

eaglet | 园豆:17139 (专家六级) | 2008-09-30 07:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册