Search code examples
asp.nethttp-redirecthttp-status-code-404response

Custom 404 page based on dynamic Url - Server.Transfer throwing error transferring to dynamic page


I capture all errors on my website and generate emails based on certain bits of inforamtion. I do this in Global.asax in Application_Error. I have implemented some code to capture 404 errors at which point it will do the following:

Server.ClearError();
string Redir = Settings["Custom404Page"];
Server.Transfer(Redir);

Redir will come out like: PageContent/123/404_Not_Found.html but when I pass this into Server.Transfer I get an error:

Error executing child request for PageContent/123/404_Not_Found.html

I don't want to use Response.Redirect as I need to ensure the status code of 404 is maintained which it doesn't seem to be when using Response.Redirect.

Does anyone have a solution to this? Am I allowed to use Server.Transfer like this?


Solution

  • You can't point Server.Transfer to a static page. You'll need to point it at a .aspx page to get it to work.

    If you really want to use an HTML page, you'll need to open the HTML file, read its contents, cache it and use Response.Write to output it while persisting the 404 response header.

    See: http://support.microsoft.com/kb/320439

    Microsoft Internet Information Services (IIS) dispatches the Server.Transfer or the Server.Execute request to the appropriate Internet Server Application Programming Interface (ISAPI) extension based on the extension of the requesting file. For example, a request for an .aspx page is dispatched to the Aspnet_isapi.dll ISAPI extension.

    After the request is dispatched to appropriate ISAPI extension, the ISAPI extension cannot call another ISAPI extension. You receive the error message that is listed in the "Symptoms" section because the Aspnet_isapi.dll file, which handles requests to ASP.NET pages, cannot forward the request to the Asp.dll file, which handles requests to ASP pages.