title: ASP.NET MVC 5 error handling
body: <p>We want to handle 403 errors, 404 errors, all errors due to a <code>MySpecialDomainException</code> and provide a default error page for all other errors (including errors in the IIS configuration!). All errors should return proper Razor views, it would be very nice to have an <code>ErrorController</code> in front of the views. <strong>E.g.</strong> something like this:</p>
  
  <pre><code>public class ErrorController : Controller
  {
      public ViewResult NotFound () { return View(); }
      public ViewResult Forbidden () { return View(); }
      public ViewResult Default ()
      {
          var ex = ObtainExceptionFromSomewhere();
          if(ex is MySpecialDomainException)
              return View("MySpecialDomainException", new ErrorModel { Exception = ex });
  
          return View("GeneralError", new ErrorModel { Exception = ex });
      }
  }
  </code></pre>
  
  <p>Currently you find many different ways to do that on the www, some most probably outdated. Among those:</p>
  
  <ul>
  <li>Controller.OnException()</li>
  <li>Error filter</li>
  <li>customErrors element in web.config</li>
  <li>Handling in Global.asax's Application_Error</li>
  </ul>
  
  <p><strong>Q1:</strong> What is the recommended way to fulfill our requirements with ASP.NET MVC 5?</p>
  
  <p>Also we want to catch errors occurring in the IIS host. <strong>Q2:</strong> To prevent that IIS has to handle any 404s we thought about adding a default route matching all possible URLs - is this recommendable? Better to register instead for IIS' 404s as well?</p>
  
  <p><strong>Q3:</strong> Is it even possible to register an IIS error page which goes back to a controller, or is IIS capable of ASPX / static HTML only?</p>