I'm working on a (Web Forms) site running on IIS 8.5 and am currently setting up error handling for 404s and 500s.
I already have the system.web
> customErrors
setup, but since we're running in Integrated mode/newer version of IIS it's pulling from system.webServer
> httpErrors
.
Using the IIS GUI I opted to replace the standard IIS error pages with a static HTML file, on the site:
<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath=""
path="/problem.html" responseMode="ExecuteURL" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="500" prefixLanguageFilePath=""
path="/problem.html" responseMode="ExecuteURL" />
</httpErrors>
Unfortunately, while the correct error page is displayed, instead of a returning a 404 a 200 is returned, and a 302 followed by a 200 is returned for 500 errors.
For older versions of IIS, or those not running in Integrated mode, you could set redirectMode="ResponseRewrite"
on the customErrors
element and that would resolve this issue.
I've tried setting existingResponse
on httpErrors
to all three available options, as well as tried updating setting responseMode="File"
, but these changed nothing.
Is there any way to do something similar for the httpErrrors
element? Or am I stuck with pointing the errors to an ASP.NET page and returning the appropriate error from there?
Thanks!
To solve this, you can set this configuration into the <httpErrors>
in your WebConfig file. Like this:
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<clear/>
<error statusCode="404" path="/WebForms/Index.aspx" responseMode="File"/>
</httpErrors>
<system.webServer/>
The responseMode=File
will preserve the original error code, and show the static page.