我只是試圖在錯誤頁面上輸出應用程序錯誤的詳細信息。在global.asax我有一些處理,然後一個server.transfer到我的網頁與此代碼:HttpContext.Current.Server.GetLastError在IIS 7中不工作
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Get exception details
Dim ex As Exception = HttpContext.Current.Server.GetLastError()
If TypeOf ex Is HttpUnhandledException AndAlso ex.InnerException IsNot Nothing Then
ex = ex.InnerException
End If
If ex Is Nothing Then
lblError.Text = "<p>An unknown error occurred.</p>"
Else
'Determine what type of error we're dealing with
If TypeOf ex Is System.Data.SqlClient.SqlException Then
'A database-related exception...
lblError.Text = String.Format("<p>The database is having problems... please try your submission again later.<br />{0}</p>", ex.Message)
ElseIf TypeOf ex Is ApplicationException Then
'An ApplicationException has occurred
lblError.Text = String.Concat("<p>An application fault has been tripped:<br /> {0}</p>", ex.Message)
Else
'For all others, display the StackTrace
lblError.Text = String.Format("<p>An unknown error occurred. The following dump of exception information can be used to diagnose the issue</p><p class=""stackTrace""><strong>Exception:</strong> {0}</p><p class=""stackTrace""><strong>Message:</strong> {1}</p><p class=""stackTrace""><strong>Stack Trace:</strong><br />{2}</p>", ex.GetType().ToString(), ex.Message, ex.StackTrace.Replace(System.Environment.NewLine, "<br />"))
End If
End If
End Sub
本地它工作的很好。當我部署到運行IIS 7的Windows Server 2008計算機時,ex的條件不會每次都被觸發。 我猜這個問題與IIS版本有關 - 只是因爲我看到了一些其他帖子,但沒有解決方案。如果有人對我如何完成錯誤消息轉儲有想法,我會很感激。我想知道爲什麼這也發生。我的意思是,一個解決方案是偉大的,但我很好奇爲什麼。