The problem
Modifying customErrors to redirect a user to a custom 404 page in the case of a fileNotFoundException will do just that - redirect the user - which means taking the detour to the client, informing of the redirect, which in turn means a 302 response from the server, before the user lands at the 404 page. The only implication of this is that search engine will fail to treat the response as a proper 404.
The solution
To resolve the issue, we have to drop the customErrors clause, and invoke Global.asax. Catching all exceptions in Global.asax, we are able to filter out the 404's, and render the appropriate site without tampering with redirects
void Application_Error(object sender, EventArgs e)
{
HttpException ex = Server.GetLastError() as HttpException;
if(ex != null && ex.GetHttpCode() == 404)
{
Response.Clear();
Response.StatusCode = 404;
Response.WriteFile("~/Pages/404.html");
Response.End();
}
}
A brief note on deployment
Finally, when deploying a project with a Global.asax file, from a web deployment project, make sure that the following files are updated:
- PrecompiledApp.config
- bin\App_global.asax.cs
- bin\App_global.asax.cs.compiled
Note that Global.asax is compiled in its entirety, and need not be uploaded to the production environment.
Inga kommentarer:
Skicka en kommentar