I use the Application_Error event to catch and log errors in my app. The Error is logged, then a friendly error screen is displayed:
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim ex As New Exception( _
String.Format("Error on page: '{0}'.", HttpContext.Current.Request.Url), _
Server.GetLastError())
Dim uid As Guid = Log.FatalError(ex)
Server.Transfer(String.Concat("~\\GlobalError.aspx?error=", uid))
End Sub
In my web.config I have:
<customErrors mode="On" defaultRedirect="GlobalError.aspx">
<error statusCode="404" redirect="PageNotFound.aspx" />
</customErrors>
Whenever a user tries to load a page that doesn't exist, they get the GlobalError.aspx page, not the PageNotFound.aspx page. I looked in the Application_Error event and found that the Response StatusCode was 200, while the Server's last error was "Page 'foo.aspx' was not found."
What do I need to do to get this working correctly?