2009-11-23 236 views
1

我已經把下面的代碼在Global.asax中Application_Error被忽略?

<%@ Application Language="VB" %> 

<script runat="server"> 

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) 
     ' Code, der beim Starten der Anwendung ausgeführt wird. 
    End Sub 


    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs) 
     ' Code, der beim Beenden der Anwendung ausgeführt wird. 
    End Sub 

    ' HelpLink Gets or sets a link to the help file associated with this exception. 
    ' InnerException Gets the Exception instance that caused the current exception. 
    ' Message Gets a message that describes the current exception. 
    ' Source Gets or sets the name of the application or the object that causes the error. 
    ' StackTrace Gets a string representation of the frames on the call stack at the time the current exception was thrown. 
    ' TargetSite Gets the method that throws the current exception. 

    ' http://www.developer.com/net/asp/article.php/961301/Global-Exception-Handling-with-ASPNET.htm 
    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) 
     ' Code, der bei einem nicht behandelten Fehler ausgeführt wird. 
     'get reference to the source of the exception chain 
     Dim ex As Exception = Server.GetLastError().GetBaseException() 

     'log the details of the exception and page state to the 
     'Windows 2000 Event Log 
     'System.Diagnostics.EventLog.WriteEntry("source", "MESSAGE: " + ex.Message + "\nSOURCE: " + ex.Source + "\nFORM: " + _ 
     'HttpContext.Current.Request.Form.ToString() + "\nQUERYSTRING: " + HttpContext.Current.Request.QueryString.ToString() + _ 
     '"\nTARGETSITE: " + ex.TargetSite.ToString + "\nSTACKTRACE: " + ex.StackTrace, System.Diagnostics.EventLogEntryType.Error) 

     Response.Redirect("GeneralError.aspx", False) 
     'Response.Redirect("GeneralError.aspx") 
     'Insert optional email notification here... 

     ''this is what we are sending 
     'Dim post_data As String = "MESSAGE: " + ex.Message + "\nSOURCE: " + ex.Source + "\nFORM: " + _ 
     'HttpContext.Current.Request.Form.ToString() + "\nQUERYSTRING: " + HttpContext.Current.Request.QueryString.ToString() + _ 
     '"\nTARGETSITE: " + ex.TargetSite.ToString + "\nSTACKTRACE: " + ex.StackTrace 
     'post_data = HttpContext.Current.Server.UrlEncode(post_data) 

     '' this is where we will send it 
     'Dim uri As String = "http://localhost/cor_raumplaner/GlobalError.aspx" 

     '' create a request 
     'Dim request As System.Net.HttpWebRequest = DirectCast(System.Net.WebRequest.Create(uri), System.Net.HttpWebRequest) 
     'request.KeepAlive = False 
     'request.ProtocolVersion = System.Net.HttpVersion.Version10 
     'request.Method = "POST" 

     '' turn our request string into a byte stream 
     'Dim postBytes As Byte() = Encoding.ASCII.GetBytes(post_data) 

     '' this is important - make sure you specify type this way 
     'request.ContentType = "application/x-www-form-urlencoded" 
     'request.ContentLength = postBytes.Length 
     'Dim requestStream As System.IO.Stream = request.GetRequestStream() 

     '' now send it 
     'requestStream.Write(postBytes, 0, postBytes.Length) 
     'requestStream.Close() 

     '' grab te response and print it out to the console along with the status code 
     'Dim response1 As System.Net.HttpWebResponse = DirectCast(request.GetResponse(), System.Net.HttpWebResponse) 

     'Response.Write(New System.IO.StreamReader(response1.GetResponseStream()).ReadToEnd()) 
     'Response.Write(response1.StatusCode) 
    End Sub 

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) 
     ' Code, der beim Starten einer neuen Sitzung ausgeführt wird. 
    End Sub 

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs) 
     ' Code, der am Ende einer Sitzung ausgeführt wird. 
     ' Hinweis: Das Session_End-Ereignis wird nur ausgelöst, wenn der sessionstate-Modus 
     ' in der Datei "Web.config" auf InProc festgelegt wird. Wenn der Sitzungsmodus auf StateServer 
     ' oder SQLServer festgelegt wird, wird das Ereignis nicht ausgelöst. 
    End Sub 

</script> 

然後我添加了一個新的頁面,並使其產生錯誤,這樣的:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    Dim mylanguage As String = Session("Language") 
    Dim iLength As Integer = mylanguage.Length 

End Sub 

現在的問題似乎是global.asax中的應用程序錯誤處理程序不會被調用,而是我收到一個標準異常: 「未將對象引用設置爲對象實例」

爲什麼?它不應該調用錯誤處理程序並重定向到GeneralError.aspx頁面嗎?

+0

使用Ctrl + K來設置您的文章的格式。將它修剪成更小的repro示例並重新發布。 – 2009-11-23 16:31:24

回答

3

在你的web.config文件中,確保你有Debug = false 如果它打開,你會看到所有的錯誤信息,你的錯誤處理程序可能無法正常工作。

+0

這對我來說沒有什麼不同,但是'Server.ClearError()'做了。 – 2016-08-26 08:35:33

3

當使用隨Visual Studio提供的AspNetDevelopmentServer時,它將忽略在繼續執行Application_Error之外錯誤未被清除之前,但在IIS中,退出該方法時該錯誤仍在會話中,因此它被視爲未被處理然後IIS將它顯示在它自己的錯誤頁面中。

要更正此問題,請在Server.GetLastError()之後使用Server.ClearError()以確保向服務器發送信號以處理錯誤。

/關注