When running Sitecore within IIS 7.5+ in Integrated Pipeline mode an IIS 404 page, instead of the Sitecore specified 404 page, is rendered when the status code is set to 404 in C#. With IIS in Classic Mode the Sitecore specified 404 page is rendered as expected as IIS is not modifying the response based upon the error status code returned by the application. Integrated Mode on Windows 2008+ server when running IIS 7.5+ will by default override the HTTP response with its default error pages if the application returns a HTTP error status code such as 404 or 500.

Default Classic Mode Behavior

Application renders the contents of the Sitecore 404 page along with setting the HTTP status to 404 as specified in C# code.

Default Integrated Mode Behavior

Application returns an HTTP status of 404 but IIS renders out the IIS 404 page instead of the Sitecore 404 page

This behavior can be validated by looking at the IIS logs through using a tool such as Log Parser Studio from Microsoft.

Modified Integrated Mode Behavior

Application renders the contents of the Sitecore 404 page along with setting the HTTP status to 404 as specified in C# code due to the TrySkipIisCustomErrors setting value.

To modify this out of the box behavior the TrySkipIisCustomErrors need to set as follows

    Response.TrySkipIisCustomErrors = true;

before setting the StatusCode to 404 as shown below

    Response.StatusCode = 404;

This allows Sitecore to process the request, handle the error, and still return a 404 Status Code with the Sitecore 404 content.