title: IIS7 and HTTP status code handling
body: <p>I'm having a hardcore head-ache from trying to get complete programmatic control over rendering of an error in IIS7 (integrated mode). What I want to do is given an error (page not found, internal server error, not authenticated, etc), transfer the whole request to a custom ASPX or HTML (I prefer the latter) with the correct HTTP status code.</p>
  
  <p>What I want is IIS7 to don't give a crap about what I set the HTTP status code to. I don't want its error handling. When I set <code>Response.StatusCode = (int)HttpStatusCode.NotFound</code>, what I want is not IIS to render its own error page, but perhaps transfer the request to another file.</p>
  
  <p>I've gotten this static configuration stuff to work:</p>
  
  <pre><code>&lt;configuration&gt;
    &lt;system.webServer&gt;
      &lt;httpErrors&gt;
        &lt;clear /&gt;
        &lt;error statusCode="404" path="/errors/404.html" responseMode="ExecuteURL" /&gt;
      &lt;/httpErrors&gt;
    &lt;/system.webServer&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>While this works, it doesn't give me programmatic control over what to do with the response, given an error scenario. Configuration is a good fallback, but I'd really like to be able to set <code>Response.StatusCode</code> and render something completely different from the configured <code>404.html</code> in certain circumstances (like a JSON response if we receive <code>Accept: application/json</code>), but IIS7 won't let me. Not a chance.</p>
  
  <p>So what the heck am I supposed to do? I've tried to set <a href="http://msdn.microsoft.com/en-us/library/system.web.httpresponse.tryskipiiscustomerrors.aspx"><code>HttpResponse.TrySkipIisCustomErrors Property</code></a>, but that looks like a huge hack and doesn't seem to work consistently. Is setting this property to true really the recommended best practice to get the behavior I want?</p>
  
  <p>At the moment, the feeling I'm left with is nothing but intense hate towards IIS7. Can anyone please help me remedy this by proving that I'm just being stupid and that I can indeed have full control over the HTTP stack?</p>