简体   繁体   中英

Custom error handling no longer working after migrating from IIS 6 to IIS 8.5

We have recently migrated one of our internal web applications using ASP.Net written in VB.Net from an old web server running Windows Server 2003 (IIS 6) to a brand new production server running Windows Server 2012 (IIS 8.5).

The migration has by and large been successful, however, since the migration our application has stopped handling errors using our error pages, instead it displays the full ASP errors in the browser for the users to see.

I have compared the website and application pool configuration (running as .Net v2.0) and they look identical (web.config files are identical).

Both of the web.config files have custom Errors set to off, which explains why full error messages would be displayed to the users, however, this should have been handled by our web application.

We have not used the custom errors page as we have a function in our global.asax file which captures the error and then passes it to the relevant error page.

This is the code which is used in 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

For some reason which I have not been able to determine IIS 8.5 is not calling this function when the error occurs and is instead displaying a verbose error on the browser. IIS 6 did not do this.

What is going wrong?

Put this code in your web config file under system.web tag.Instead of using the global.asax file.In the custom error itself you can redirect to your error page for the respective error codes.

 <customErrors mode="On" defaultRedirect="inconvience.html">
 <error statusCode="404" redirect="pagenotfound.html"/>
 <error statusCode="500" redirect="page1.html"/>
 </customErrors>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM