【发布时间】:2015-08-11 15:35:11
【问题描述】:
我们最近使用 VB.Net 编写的 ASP.Net 将我们的一个内部 Web 应用程序从运行 Windows Server 2003 (IIS 6) 的旧 Web 服务器迁移到运行 Windows Server 2012 (IIS 8.5) 的全新生产服务器。
迁移大体上是成功的,但是,自从迁移以来,我们的应用程序已经停止使用我们的错误页面处理错误,而是在浏览器中显示完整的 ASP 错误供用户查看。
我比较了网站和应用程序池配置(运行为 .Net v2.0),它们看起来相同(web.config 文件相同)。
两个 web.config 文件都将自定义错误设置为关闭,这解释了为什么会向用户显示完整的错误消息,但是,这应该由我们的 Web 应用程序处理。
我们没有使用自定义错误页面,因为我们的 global.asax 文件中有一个函数可以捕获错误,然后将其传递给相关的错误页面。
这是 global.asax 中使用的代码:
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Fires when an error occurs
Try
Dim blnSendCustomErrorPage As Boolean = SendCustomErrorPages()
Dim strAuditMessage As String
Dim ex As Exception = Server.GetLastError.GetBaseException
Dim lngCallID As Long = -1
Dim lngAuditLogID As Long = -1
If Not CurrentUser.WorkingSession Is Nothing Then
lngCallID = CurrentUser.WorkingSession.CallID
End If
If Not ex Is Nothing Then
If ex.GetType() Is GetType(System.IO.FileNotFoundException) Then
strAuditMessage = ex.Message
lngAuditLogID = WebAuditLog.Write( _
WebAuditLog.AuditLookup.PageNotFoundException, strAuditMessage, _
MethodBase.GetCurrentMethod().MethodHandle, lngCallID)
If Response.IsClientConnected AndAlso blnSendCustomErrorPage Then
Response.Redirect("~/error/PageNotFound.aspx?AuditLogID=" & lngAuditLogID, True)
End If
ElseIf ex.GetType Is GetType(System.UnauthorizedAccessException) Then
strAuditMessage = ex.Message
lngAuditLogID = WebAuditLog.Write( _
WebAuditLog.AuditLookup.AccessNotAllowedException, strAuditMessage, _
MethodBase.GetCurrentMethod().MethodHandle, lngCallID)
If Response.IsClientConnected AndAlso blnSendCustomErrorPage Then
Response.Redirect("~/error/NoAccess.aspx?AuditLogID=" & lngAuditLogID, True)
End If
Else
Dim blnWriteToLog As Boolean = True
If ex.GetType Is GetType(SqlClient.SqlException) Then
Dim exSql As SqlClient.SqlException = DirectCast(ex, SqlClient.SqlException)
If exSql.Number = 17 Then '(SQL Server does not exist or access denied.)
blnWriteToLog = False
End If
End If
strAuditMessage = WebAuditLog.GetExceptionSummary(ex)
If blnWriteToLog Then
lngAuditLogID = WebAuditLog.Write( _
WebAuditLog.AuditLookup.ApplicationErrorException, strAuditMessage, _
MethodBase.GetCurrentMethod().MethodHandle, lngCallID)
Else
lngAuditLogID = -1
End If
If Response.IsClientConnected AndAlso blnSendCustomErrorPage Then
Response.Redirect("~/error/ApplicationError.aspx?AuditLogID=" & lngAuditLogID) '& _
'"&Message=" & HttpUtility.UrlEncode(ex.Message), True)
End If
End If
Else
strAuditMessage = "Server.GetLastError.GetBaseException Is Nothing"
lngAuditLogID = WebAuditLog.Write( _
WebAuditLog.AuditLookup.ApplicationErrorException, strAuditMessage, _
MethodBase.GetCurrentMethod().MethodHandle, lngCallID)
If Response.IsClientConnected AndAlso blnSendCustomErrorPage Then
Response.Redirect("~/error/ApplicationError.aspx?AuditLogID=" & lngAuditLogID, True)
End If
End If
Notification.Notify(Notification.NotificationType.Email, _
"Application Error AuditLogID " & lngAuditLogID.ToString & " " & _
User.Identity.Name & System.Environment.NewLine & strAuditMessage, "", , "BOCSS Error " & User.Identity.Name)
Catch exApplication_Error As Exception
'TODO: Decide what to do. -> Write to text file
End Try
End Sub
由于某种原因,我无法确定 IIS 8.5 在发生错误时没有调用此函数,而是在浏览器上显示详细错误。 IIS 6 没有这样做。
出了什么问题?
【问题讨论】:
-
global.asax中的其他函数调用正常吗? -
是的,global.asax 中的所有其他功能(例如用户身份验证)都按预期工作。
-
@KamalBoucetla 您是否设法为此找到解决方案?我的一位同事遇到了类似的问题,这是 Google 返回的 Stack Overflow 结果! :)。