IIS Overwriting HTTP Response Text When HTTP Response Status set as 400

At least the page Configuring HTTP Error Responses in IIS 7 says

Note

You cannot customize the following HTTP error messages: 400, 403.9, 411, 414, 500, 500.11, 500.14, 500.15, 501, 503, and 505.

EDIT: though in the responses of this question, which looks quite similar, there is a response claiming that at least something can be done with httpErrors configuration.


In MVC it is also possible to do this on a Action by Action basis. See TrySkipisCustomErrors.

Use:

Response.TrySkipIisCustomErrors = true;
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return this.Json(SomeJsonObject);  // could output string too

we ended up with going for

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

in the web.config. Which allowed custom errors that set status codes to leave IIS unmolested.


After talking to a friend of mine who is a wiz at configuring IIS, I found that in IIS 7+ you can add the following to web.config:

<system.webServer>
  <httpErrors errorMode="Detailed"/>
</system.webServer>

If this setting in web.config is used, AND you set the body of the response, then the response body will reach the client. If you do NOT set the response body, then IIS will serve up a detailed error page with detailed error information (see http://learn.iis.net/page.aspx/267/how-to-use-http-detailed-errors-in-iis/). Many folks consider this a security risk, so use with caution.