2

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.

kenwarner
  • 28,650
  • 28
  • 130
  • 173
  • Make sure to add a call to your error logging system. If an unhandled exception happens, you want to know so you can go handle it. – tsilb Mar 28 '10 at 01:44

3 Answers3

4

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.

Bryan
  • 8,748
  • 7
  • 41
  • 62
  • this is essentially what was happening. server.transfer was throwing an exception because its output buffer was not empty – kenwarner Mar 30 '10 at 00:57
0

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.

LesterDove
  • 3,014
  • 1
  • 23
  • 24
  • if I use Response.Redirect it generates a HTTP 302 and then redirects to error_404.aspx. I want to generate a single 404 request and display the contents of the error_404.aspx while keeping the url the same as the original reqest. per your second comment, how am I asking IIS to render the global.asax? I thought Server.Transfer("error_404.aspx") means it would ask IIS to render error_404.aspx – kenwarner Mar 27 '10 at 03:08
  • Makes sense.. I managed to make two mistakes in that small post. Perhaps I'll delete and await the real answer... – LesterDove Mar 27 '10 at 03:28
0

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.

JimmyPena
  • 8,694
  • 6
  • 43
  • 64
anishMarokey
  • 11,279
  • 2
  • 34
  • 47
  • like i said to LesterDove, if I use a Response.Redirect, I end up with 2 responses. The original page, which 302 redirects to error_404.aspx. I want to generate a single 404 response – kenwarner Mar 27 '10 at 14:10