IIS is overriding my response content, if I manually set the Response.StatusCode

Solution 1:

Ok - found the answer. As I expected, IIS is hijacking my non 200 responses. Not sure (ie. I'm not sure if this is the default behaviour OR it's because of a setting one of the team members updated in the machine config, etc...).

Anyways, the key here is tell IIS to not handle any non-200 status result resources.

How? Config entry in the web.config.

<system.webServer>
    <httpErrors errorMode="DetailedLocalOnly" existingResponse="PassThrough"/>
    .... snipped other IIS relevant elements ... 
</system.webServer>

Now, the key here is existingResponse="PassThrough" . That bad boy tells IIS to leave my resources alone if the HTTP status code != 200.

Want more info? Sure: Read More about this Element on the Official IIS Website.

Solution 2:

Another way to bypass this is to run the following code in your ASP application:

Response.TrySkipIisCustomErrors = true;

Source: https://stackoverflow.com/a/21271085/238753