0

I am simply trying to output the details of the application error on my error page. In global.asax I have some handling and then a server.transfer to my page with this code:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Get exception details
    Dim ex As Exception = HttpContext.Current.Server.GetLastError()

    If TypeOf ex Is HttpUnhandledException AndAlso ex.InnerException IsNot Nothing Then
        ex = ex.InnerException
    End If

    If ex Is Nothing Then
        lblError.Text = "<p>An unknown error occurred.</p>"
    Else
        'Determine what type of error we're dealing with
        If TypeOf ex Is System.Data.SqlClient.SqlException Then
            'A database-related exception...
            lblError.Text = String.Format("<p>The database is having problems... please try your submission again later.<br />{0}</p>", ex.Message)
        ElseIf TypeOf ex Is ApplicationException Then
            'An ApplicationException has occurred
            lblError.Text = String.Concat("<p>An application fault has been tripped:<br /> {0}</p>", ex.Message)
        Else
            'For all others, display the StackTrace
            lblError.Text = String.Format("<p>An unknown error occurred. The following dump of exception information can be used to diagnose the issue</p><p class=""stackTrace""><strong>Exception:</strong> {0}</p><p class=""stackTrace""><strong>Message:</strong> {1}</p><p class=""stackTrace""><strong>Stack Trace:</strong><br />{2}</p>", ex.GetType().ToString(), ex.Message, ex.StackTrace.Replace(System.Environment.NewLine, "<br />"))
        End If
    End If

End Sub

Locally it works great. When I deploy to our Windows Server 2008 machine running IIS 7, the condition for ex is nothing is tripped every time. I'm guessing that this issue is related to the IIS version - simply because I saw a few other posts along those lines, but no solutions. If anyone has thoughts as to how I may accomplish my error message dump, I'd appreciate it. I would like to know why this is happening also. I mean, a fix is great but I am curious as to why.

Michael
  • 406
  • 1
  • 7
  • 12

2 Answers2

0

There was a very similar known bug in Vista which was supposedly fixed in SP1, but it looks like the same fix isn't part of Windows 2008 Server yet. There is a workaround, though - if you set the site's default error property (under IIS settings -> Error Pages -> Edit Feature Settings...)to the custom page (see below), IIS will invoke this page whenever an error is not handled by an explicitly configured status-code handler (so your 404, etc. handlers will still work) - but for some reason, handling the error this way means Server.GetLastError() still works properly.

See original blog post: Fun with Server.GetLastError() in classic ASP on Windows Server 2008.

I suppose it is similar to your case and may help you too.

Serge S.
  • 4,505
  • 3
  • 36
  • 45
0

Another approach is to use in web.config a new parameter redirectMode introduced in ASP.NET 3.5 SP1.

The redirectMode="ResponseRewrite" mode allows to load the Error Page without redirecting the browser, URL stays the same, and exception will not be lost.

It is answered on ASP.NET custom error page - Server.GetLastError() is null question. See it for more details.

Community
  • 1
  • 1
Serge S.
  • 4,505
  • 3
  • 36
  • 45