HttpHandler for 404 error stopped working after 4.0 upgrade
I have an HttpHandler which takes care of 404 errors, which I implemented years ago as a way of doing url routing. If the request maps to a valid page, it redirects to that page. Otherwise, it returns a 404. (I know I should start using the new routing feature of asp.net 4.0, but that will take some time. I need to get this working asap.)
In IIS6, I have mapped the 404 error to "/404.ashx". In the web.config, custom errors are set up like so:
<customErrors mode="On" defaultRedirect="error.aspx">
<error statusCode="404" redirect="/404.ashx"/>
</customErrors>
and the http handler:
<httpHandlers>
<add verb="GET" path="404.ashx" type="myNamespace.PageNotFoundHandler,myAssemblyName"/>
</httpHandler开发者_StackOverflow中文版s>
This has been working for years - it stopped working as soon as I changed the site to use asp.net 4.0. Everything was recompiled for 4.0, and there were no code changes.
Now, when I hit one of these urls that used to work, I get a blank page with a 404 error code. If I remove the IIS 404 error mapping, I get a regular old 404 page. It seems that the httphandler is not being called.
I have other http handler which are working fine.
I have set the EnableExtensionlessUrls item in the registry to 0, as suggested here and other places.
I figure there must be some configuration setting I missed or something like that. Naturally, this is a problem only on my production server, so I can't run it in the debugger to see what is happening. If nothing easy comes up, I will insert some extra logging in my system to help track it down.
Any ideas on what I else can check?
You might check to see if it's the Verb attribute, maybe it should be POST or *?
Another quick fix would be to add code to your Application_Error event handler in your global class like so.
for (Ex = Server.GetLastError(); Ex != null; Ex = Ex.InnerException)
{
if (Ex is HttpException && ((HttpException)Ex).GetHttpCode() == 404 && Context != null)
{
Context.Server.Transfer("~/404.ashx");
return;
}
}
This would remove the need for any configuration settings inside of your web config, but it might behave somewhat differently than what you currently use as it would take on the page identity of the currently requested page/file instead of passing along the path in a query string as the normal IIS 404 custom error page is handled.
Changing from the default app pool to the classic one fixed it for me.
精彩评论