title: How to prevent IIS7 for handling HTTP status code 401?
body: <p>I'm working my ASP.NET MVC 2 project. I create exception filter for catching unauthorized access exception that occur when user does not has permission to view some action.</p>
  
  <pre><code>[CustomError(typeof(UnauthorizedAccessException), "Error", "UnauthorizedAccess")]
  public class MyController : BaseController
  {
  }
  </code></pre>
  
  <p>After exception has been thrown, my filter will transfer to configured controller/action that is the following method.</p>
  
  <pre><code>public ActionResult UnauthorizedAccess(ExceptionContext context)
  {
      Response.StatusCode = CustomHttpStatusCode.UnauthorizedUser;
  
      return View(model);
  }
  </code></pre>
  
  <p>Finally, before ASP.NET application end this request, it will call the following method that located in Global.ascx for changing custom HTTP status code to HTTP status 401(unauthorized access).</p>
  
  <pre><code>public void Application_EndRequest(object sender, EventArgs e)
  {
      if (Response.StatusCode == CustomHttpStatusCode.UnauthorizedUser)
      {
          Response.StatusCode = 401;
      }
  }
  </code></pre>
  
  <p>Everything is work fine on my machine (IIS 7.5). But it does not work on my deploy website. It still return plain text <strong>"You do not have permission to view this directory or page."</strong> instead of my custom error page.</p>
  
  <p>PS. The following config is my current web.config for this case.</p>
  
  <pre><code>  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
    &lt;configuration&gt;
      &lt;system.web&gt;
        &lt;customErrors mode="On"&gt;&lt;/customErrors&gt;
      &lt;/system.web&gt;
      &lt;system.webServer&gt;
        &lt;httpErrors errorMode="Custom"&gt;
           &lt;remove statusCode="502" subStatusCode="-1" /&gt;
           &lt;remove statusCode="501" subStatusCode="-1" /&gt;
           &lt;remove statusCode="500" subStatusCode="-1" /&gt;
           &lt;remove statusCode="412" subStatusCode="-1" /&gt;
           &lt;remove statusCode="406" subStatusCode="-1" /&gt;
           &lt;remove statusCode="405" subStatusCode="-1" /&gt;
           &lt;remove statusCode="404" subStatusCode="-1" /&gt;
           &lt;remove statusCode="403" subStatusCode="-1" /&gt;
           &lt;remove statusCode="401" subStatusCode="-1" /&gt;
        &lt;/httpErrors&gt;
      &lt;/system.webServer&gt;
    &lt;/configuration&gt;
  </code></pre>