title: ASP.NET MVC Custom Errors
body: <p>My goal is to create an error handling within the application that handles all managed errors, not just MVC related. So I'm not using the HandleErrorAttribute pattern because it is only for MVC errors, not other managed errors. </p>
  
  <p>In my Global.asax I have:</p>
  
  <pre><code>protected void Application_Error()
  {
      string displayError = ConfigurationManager.AppSettings["DisplayError"];
      NameValueCollection serverVariables;
      int loop1, loop2;
      StringBuilder serverVariableKeys = new StringBuilder();
      Exception exception = Server.GetLastError();
      HttpException httpException = exception as HttpException;
  
      if (HttpContext.Current != null)
      {
              // loop through and get server vars
      }
  
      if (exception != null)
          // Log Error
  
      // Redirect to Error page if set to basic mode
      if (!string.IsNullOrWhiteSpace(displayError) &amp;&amp; displayError.ToLower() == "basic")
      {
          Response.Clear();
      Server.ClearError(); // needed for redirect to work
  
      var routeData = new RouteData();
      routeData.Values["controller"] = "Error";
      routeData.Values["action"] = "General";
      routeData.Values["exception"] = exception;
      Response.StatusCode = 500;
  
      if (httpException != null)
      {
         Response.StatusCode = httpException.GetHttpCode();
         switch (Response.StatusCode)
          {
          case 403:
            routeData.Values["action"] = "Http403";
            break;
              case 404:
             routeData.Values["action"] = "Http404";
             break;
          }
      }
  
      IController errorsController = new Controllers.ErrorController();
      var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
      errorsController.Execute(rc);
      }
  }
  </code></pre>
  
  <p>I have an <code>ErrorController</code> that has <code>General</code>, <code>Http403</code> and <code>Http404</code> Actions. I have views that correspond to each action. </p>
  
  <p>In my web.config I have:</p>
  
  <pre class="lang-xml prettyprint-override"><code>  &lt;system.web&gt;  
      &lt;!-- removed bits to reduce space --&gt;
      &lt;customErrors mode="On" /&gt;
    &lt;/system.web&gt;
    &lt;system.webServer&gt;
      &lt;validation validateIntegratedModeConfiguration="false" /&gt;
      &lt;modules &gt;
        &lt;remove name="UrlRoutingModule-4.0" /&gt;
        &lt;add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" /&gt;
        &lt;remove name="Session"/&gt;
        &lt;add name="Session" type="System.Web.SessionState.SessionStateModule" preCondition=""/&gt;
      &lt;/modules&gt;
      &lt;handlers&gt;
        &lt;remove name="ExtensionlessUrlHandler-Integrated-4.0" /&gt;
        &lt;add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /&gt;
      &lt;/handlers&gt;
      &lt;httpErrors errorMode="Detailed" /&gt;
    &lt;/system.webServer&gt;
  </code></pre>
  
  <p>This works for many scenarios related to managed code, but when IIS takes over, such as bad static file call (example.com/BadFileName.gif) it will return an error from IIS that discloses server details such as local file path. This is because of the following line in the web.config:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;httpErrors errorMode="Detailed" /&gt;
  </code></pre>
  
  <p>But when I set errorMode to other values such as Custom, or just remove the line, my custom error handling stops functioning and the default IIS errors messages are returned for everything including managed code errors. </p>
  
  <p>I've tried a number of things to get to the Custom Errors to work without this setting without any luck, including:</p>
  
  <ul>
  <li>Setting <code>CustomError</code> to <code>mode="Off"</code></li>
  <li>Defining Error nodes under <code>customError</code> to redirect to my controllers</li>
  <li>Defining Error nodes under <code>httpErrors</code> to redirect to my controllers</li>
  </ul>
  
  <p>When I step through in debugging I can see the call to the controller, but in the end IIS sends the response instead. The error logging does get called correctly Application_Error, so the code does let application handle it to certain point. </p>
  
  <p>Application Details:</p>
  
  <ul>
  <li>ASP.NET MVC 4.0</li>
  <li>Hosted on IIS 7.5 (Azure Web Site Preview)</li>
  </ul>
  
  <p>Is there way not use <code>&lt;httpErrors errorMode="Detailed" /&gt;</code> and make this work?</p>