title: handle 404, 403, 500 http codes in ASP.NET MVC 5
body: <p>I have spent the whole day trying to implement custom handler for HTTP errors.
  I am using MVC 5 and IIS 7. There are a lot of good suggestions <a href="https://stackoverflow.com/questions/619895/how-can-i-properly-handle-404-in-asp-net-mvc">HERE</a>. Since I need to take care not only for 404 I have tried <a href="https://stackoverflow.com/questions/619895/how-can-i-properly-handle-404-in-asp-net-mvc/620559#620559">THIS</a> option.</p>
  
  <p>Ultimately I need to be able to handle all these cases</p>
  
  <p>Should be able to handle all unmatched routes
      localhost/404_test/test/test/test/test/12/23</p>
  
  <p>Should be able to handle HTTP 500
      throw exception from the action </p>
  
  <p>Should be able to handle all html errors for example 400.
      localhost/404_test/ddsa
  
  <p>Should be able to handle "file extension like url"
      localhost/404_test/test.cdd</p>
  
  <p>Using the code in the provided link also by updating web.config as shown below I am able to handle all cases except "file extension like url". I am receiving blank page. It looks like IIS is overwriting response. It was the same for unmatched routes before I added </p>
  
  <pre><code>&lt;httpErrors errorMode="Custom" existingResponse="PassThrough" &gt;
  </code></pre>
  
  <p>Here is the rout map for unmatched cases :</p>
  
  <pre><code>        routes.MapRoute("NotFound", "{*url}",
              new { controller = "Error", action = "NotFound" });
  </code></pre>
  
  <p>Any suggestions how to have this implemented ? I see stackoverflow.com works exactly the way I want to have, but I have no idea how it is implemented.</p>
  
  <p><strong>Web.config</strong> </p>
  
  <pre><code>&lt;system.webServer&gt;
      &lt;modules runAllManagedModulesForAllRequests="true" /&gt;
       &lt;httpErrors errorMode="Custom" existingResponse="PassThrough" &gt;
        &lt;remove statusCode="404" subStatusCode="-1" /&gt;
        &lt;error statusCode="404" subStatusCode="-1" responseMode="ExecuteURL" path="/Error/NotFound" /&gt;
        &lt;remove statusCode="500" subStatusCode="-1" /&gt;
        &lt;error statusCode="500" subStatusCode="-1" responseMode="ExecuteURL" path="/Error/ServerError" /&gt;
      &lt;/httpErrors&gt;
    &lt;/system.webServer&gt;
  </code></pre>