4

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!

James Skemp
  • 8,018
  • 9
  • 64
  • 107

1 Answers1

5

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.

Pedro Benevides
  • 1,970
  • 18
  • 19
  • 1
    Looks like the combination of `existingResponse="Replace"` and `responseMode="File"` was the trick. However, for a static HTML I also had to drop the initial slash, `path="problem.html"` (otherwise it was returning a white page error). I also had to clear out the `customErrors` element (well, mode just set to `RemoteOnly`) – James Skemp Dec 29 '15 at 16:51
  • You can also use `defaultResponseMode="File"` on the `httpErrors` element to avoid declaring it on multiple `error` nodes –  Jan 11 '17 at 10:28