Monday, December 22, 2008

 

Recovering the Missing

Under IIS 5.0 and before, and when debugging under Visual Studio 2005, HTTP file-not-found errors can be caught in a global.asax Application_Error event function, obscurely documented by Microsoft, whose first argument can be cast to global_asax, which supplies context, including an error message but not an HTTP error code. By using Server.Transfer error handling can be redirected to an ASP.NET page.

Microsoft documentation for Visual Studio 2005 does not explain how to catch HTTP file-not-found errors under IIS 6.0, and many of the contributors to forums and Web logs have spread misinformation. IIS 6.0 will redirect HTTP file-not-found errors when configured for Custom Errors: HTTP Error 404, Type URL and Contents a so-called "absolute URL" for an ASP.NET page, for example, /VirtualDirectory/ErrorHandler.aspx where ErrorHandler.aspx.cs provides code to handle the file-not-found error.

The key to this approach, omitted from Microsoft documentation, is that Request.RawUrl in the code for the error handler page identifies the HTTP error and provides the original URL that provoked it. It has the format, ErrorHandlerURL?nnn;OriginalURL, where ErrorHandlerURL is the URL to reach the error handler page, nnn is the HTTP error code, and OriginalURL is the URL that provoked the HTTP error. An error handler page can set Response.ContentType and write to the Response.Output stream to supply content for the OriginalURL.

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]