这么做的:
做个页面的基类,所有页面都从它继承,在基页里做统一的错误处理,重要代码这样的:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Load += new System.EventHandler(this.PhilePage_Load);
this.Error += new System.EventHandler(this.PhilePage_Error);
}
.................
显示错误信息如下:
protected void PhilePage_Error(object sender, System.EventArgs e)
{
string errMsg;
Exception currentError = Server.GetLastError(); errMsg = "<link rel=\"stylesheet\" href=\"/ThePhile/Styles/ThePhile.CSS\">";
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();
}

解决方案 »

  1.   

    试试
    <customErrors defaultRedirect = "allErrors.aspx" mode="Off" >
        <error statusCode="404" redirect="404Errors.aspx"/>
        <error statusCode="405" redirect="405Errors.aspx"/>
    </customErrors>
      

  2.   

    Global Exception Handling with ASP.NET
    http://www.developer.com/net/asp/article.php/961301
    http://www.codeproject.com/aspnet/errorhandling.asp
      

  3.   

    以下示例显示 Web.config 文件中的典型 customErrors 块。 
    <customErrors mode="RemoteOnly" defaultRedirect="AppErrors.aspx"> 
       <error statusCode="404" redirect="NoSuchPage.aspx"/> 
       <error statusCode="403" redirect="NoAccessAllowed.aspx"/> 
    </customErrors>
      

  4.   

    /// <summary>
    /// 错误处理函数,用于记录错误日志
    /// </summary>
    public class Error {
    //记录错误日志位置
    private const string FILE_NAME = "c:\\udslog.txt";/// <summary>
    /// 记录日志至文本文件
    /// </summary>
    /// <param name="message">记录的内容</param>
    public static void Log(string message) {
    if(File.Exists(FILE_NAME))
    {
    StreamWriter sr = File.AppendText(FILE_NAME);
    sr.WriteLine ("\n");
    sr.WriteLine (DateTime.Now.ToString()+message);
    sr.Close();
    }
    else
    {
    StreamWriter sr = File.CreateText(FILE_NAME);
    sr.Close();
    }在具体页面中调用:
    try
    {
      有可能出错的代码...
    }catch(Exception ex)
    {
    Response.Write(ex.ToString());
    UDS.Components.Error.Log(ex.ToString());
    }
      

  5.   

    我同意我楼上的,方法,用 try catch 传送错误到页。
    比如
    try
    {
      有可能出错的代码...
    }catch(Exception ex)
    {
             string err=ex.ToString();
    Response.Write("error.aspx?Error="err);

    }
    然后 在你的error.aspx 中用 Request.QueryString["Error"];
      

  6.   

    这个问题挺复杂的就像 smilnet(笨笨) 说的你要自己定义错误ID,也就是说要自己进行异常处理,你要考虑所以的出错的条件,进行捕获也就是实现了相当于框架里的异常处理机制一样!
      

  7.   

    我用
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here
            lblErrors.Text = Server.GetLastError().ToString()    End Sub不行
      

  8.   

    给错误页传递错误信息,然后打印出来不就OKerror.aspx?err="....."error.aspxString s = Request["err"].ToString();
    Response.Write(s);
      

  9.   

    前提是要捕捉错误并注意一些无法预料的错误用Try...catch
      

  10.   

    1 我不用Try...catch
    2 我用的是 在web。config中改
    <customErrors mode="RemoteOnly" defaultRedirect="Error.aspx" />
    3。我想把错误显示在错误页上面
    4。用这个 lblErrors.Text = Server.GetLastError().ToString()不行大家看清问题 ,先谢谢各位了
      

  11.   

    在WEB CONFIG 中统一设定
      

  12.   

    如何看log是不是在系统工具的事件查看器里
      

  13.   

    [C#] 
    Exception LastError;
    String ErrMessage;
     
    LastError = Server.GetLastError();if (LastError != null)
       ErrMessage = LastError.Message;
    else
       ErrMessage = "No Errors";Response.Write("Last Error = " + ErrMessage);
      

  14.   

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here
            ' lblErrors.Text = Request.QueryString("str")
            lblErrors.Text = Server.GetLastError().ToString()        Server.ClearError()    End Sub为什么不行啊
    我都急死了
      

  15.   

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here
            ' lblErrors.Text = Request.QueryString("str")
            Dim LastError As Exception
            Dim ErrMessage As String        LastError = Server.GetLastError()        If Not LastError Is Nothing Then
               ErrMessage = LastError.Message
            Else
               ErrMessage = "No Errors"
            End If
            lblErrors.Text = ErrMessage         Server.ClearError()    End Sub
      

  16.   

    wangsaokui(无间道II(前传)) 为什么是 No Error 
    捕获不着 错误信息
      

  17.   

    我不清楚你说的“错误页”指的是404之类的IIS能返回的“错误页”,还是单纯是asp.net的500服务器错误的asp.net“调试信息页”1.如果你要改“错误页”,有如下方法:
    1.1.在IIS里面修改404等错误对应的页面指向;
    1.2.用httpHandler和/或httpModule检测整个站点,遇到非200返回就根据ResultCode返回对应错误页;
    1.3.修改web.config,详细请看楼上别人的回答。2.如果你要修改asp.net“调试信息页”:
    我不建议你在你的页面工程内做,因为这样如果你的调试页也出错了就很麻烦。asp.net中向调试页、web service页这些特殊页面都是有特定的页面处理的,如果你要改,就先参考系统的调试页来写自己的调试页,然后在web.config或者machine.config中把asp.net调试页指向你自己的。这些查一下MSDN应该都有的。