IIS7 Overrides customErrors when setting Response.StatusCode

asp.netiis-7

Having a weird problem here. Everybody knows that if you use web.config's customErrors section to make a custom error page, that you should set your Response.StatusCode to whatever is appropriate. For example, if I make a custom 404 page and name it 404.aspx, I could put <% Response.StatusCode = 404 %> in the contents in order to make it have a true 404 status header.

Follow me so far? Good. Now try to do this on IIS7. I cannot get it to work, period. If Response.StatusCode is set in the custom error page, IIS7 seems to override the custom error page completely, and shows its own status page (if you have one configured.)

Has anyone else seen this behavior and also maybe know how to work around it? It was working under IIS6, so I don't know why things changed.

Note: This is not the same as the issue in ASP.NET Custom 404 Returning 200 OK Instead of 404 Not Found

Best Answer

Set existingResponse to PassThrough in system.webServer/httpErrors section:

  <system.webServer>
    <httpErrors existingResponse="PassThrough" />
  </system.webServer>

Default value of existingResponse property is Auto:

Auto tells custom error module to do the right thing. Actual error text seen by clients will be affected depending on value of fTrySkipCustomErrors returned in IHttpResponse::GetStatus call. When fTrySkipCustomErrors is set to true, custom error module will let the response pass through but if it is set to false, custom errors module replaces text with its own text.

More information: What to expect from IIS7 custom error module