Question

I look for HttpExceptions in the Application_Error sub of my global.asx

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

        Dim ex As Exception = HttpContext.Current.Server.GetLastError()

        If ex IsNot Nothing Then
            If TypeOf (ex) Is HttpUnhandledException Then
                If ex.InnerException Is Nothing Then
                    Server.Transfer("error.aspx", False)
                End If
                ex = ex.InnerException
            End If

            If TypeOf (ex) Is HttpException Then
                Dim httpCode As Integer = CType(ex, HttpException).GetHttpCode()
                If httpCode = 404 Then
                    Server.ClearError()
                    Server.Transfer("error_404.aspx", False)
                End If
            End If
        End If
End Sub

I can step through this code and confirm it does hit the Server.Transfer("error_404.aspx"), as well as the Page_Load of error_404.aspx, but all it shows is a blank page.

Was it helpful?

Solution

Are you clearing the Response buffer? You have no clue what is already there, since you are doing this in the Application_Error catch-all. Server.Transfer just appends whatever the new page generates onto the existing Response. Obviously this could create some issues.

OTHER TIPS

Does it work if you change the Server.Transfer to a Response.Redirect? (You may have to use the HTTPContext.Current prefix from where you are in global.asax.)

I'm not sure that a Server.Transfer is a good idea in the context of what you're doing, since you're effectively asking IIS to render the global.asax URL to the browser.

I think if some error occurs, Server.Transfer won't fire.

Server.Transfer is not a good method for this. Try with Response.Redirect. It should work.

If you have any exception is there any requirement for maintaining the states? If not, go with Response.Redirect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top