CustomErrors does not work when setting redirectMode="ResponseRewrite"

Eduardo Molteni picture Eduardo Molteni · Apr 23, 2009 · Viewed 61.8k times · Source

In a old site, I was changing the way that CustomErrors works by adding redirectMode="ResponseRewrite" (new in 3.5 SP1):

<customErrors mode="RemoteOnly" defaultRedirect="Error.aspx" redirectMode="ResponseRewrite">
    <error statusCode="404" redirect="404.aspx" />
</customErrors> 

The thing is: it shows me the generic error page (the one that you get when you don't set customErrors. If I remove theredirectMode="ResponseRewrite" part, it works fine.

I'm sure 3.5 SP1 is installed in the server, because I use the same setting on other sites hosted in the same server.

Any ideas?

Answer

Michael Hallock picture Michael Hallock · Sep 22, 2010

It is important to note for anyone trying to do this in an MVC application that ResponseRewrite uses Server.Transfer behind the scenes. Therefore, the defaultRedirect must correspond to a legitimate file on the file system. Apparently, Server.Transfer is not compatible with MVC routes, therefore, if your error page is served by a controller action, Server.Transfer is going to look for /Error/Whatever, not find it on the file system, and return a generic 404 error page!