views:

364

answers:

3

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.

A: 

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
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
qntmfred
Makes sense.. I managed to make two mistakes in that small post. Perhaps I'll delete and await the real answer...
LesterDove
A: 

I think if some error comes it wont fire Server.Transfer.

[Server.Transfer][1] is not a good method for this .Try with [Response.Redirect][2].I should work.

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

anishmarokey
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
qntmfred
+3  A: 

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
this is essentially what was happening. server.transfer was throwing an exception because its output buffer was not empty
qntmfred