title: Custom Error pages in MVC 5
body: <p>I have been trying to configure Custom Error pages in ASP.NET MVC 5 but with little complete success.</p>
  
  <p>To do this, I have followed this guide: <a href="http://benfoster.io/blog/aspnet-mvc-custom-error-pages" rel="nofollow noreferrer">http://benfoster.io/blog/aspnet-mvc-custom-error-pages</a></p>
  
  <p>Web.config <code>customerrors</code></p>
  
  <pre><code>&lt;customErrors mode="On" defaultRedirect="~/500.aspx" redirectMode="ResponseRewrite"&gt;
    &lt;error statusCode="401" redirect="~/401.aspx" /&gt;
    &lt;error statusCode="403" redirect="~/401.aspx" /&gt;
    &lt;error statusCode="404" redirect="~/404.aspx" /&gt;      
  &lt;/customErrors&gt;
  </code></pre>
  
  <p>Web.config <code>httpErrors</code></p>
  
  <pre><code>&lt;httpErrors errorMode="Custom" &gt;
    &lt;remove statusCode="401" /&gt;
    &lt;error statusCode="401" path="401.html" responseMode="File" /&gt;
    &lt;remove statusCode="403" /&gt;
    &lt;error statusCode="403" path="401.html" responseMode="File" /&gt;
    &lt;remove statusCode="404" /&gt;
    &lt;error statusCode="404" path="404.html" responseMode="File"/&gt;
    &lt;remove statusCode="500" /&gt;
    &lt;error statusCode="500" path="500.html" responseMode="File" /&gt;
  &lt;/httpErrors&gt;
  </code></pre>
  
  <p>I have removed the <code>HandleErrorAttribute</code> from filter config.</p>
  
  <p>With this setup, I get the following results:</p>
  
  <ul>
  <li><p><strong>Browse to a non-existent static resource</strong><br>
  Example: <a href="http://my.website.com/fakeresource.htm" rel="nofollow noreferrer">http://my.website.com/fakeresource.htm</a><br>
  This results in the static 404.htm file configured in the httpErrors section.<br>
  The Url is preserved in the browser address bar<br>
  A 404 status code is correctly set.</p></li>
  <li><p><strong>Browse to a bad route</strong><br>
  Example: <a href="http://my.website/realcontroller/fakeaction" rel="nofollow noreferrer">http://my.website/realcontroller/fakeaction</a><br>
  This results in a <code>HttpException</code> being thrown when a matching route cannot be found.<br>
  The 404.aspx page is displayed.<br>
  The Url is preserved in the browser address bar.<br>
  A 404 status code is correctly set.</p></li>
  <li><p><strong>Browse to a valid route, but with an invalid ID for a database resource</strong><br>
  Example: <a href="http://my.website.com/realcontroller/realaction/22" rel="nofollow noreferrer">http://my.website.com/realcontroller/realaction/22</a><br>
  (22 is the ID of a resource which does not exist, the Action will return the <code>HttpNotFound()</code> method.  <br>
  This results in the static 404.htm file configured in the httpErrors section.<br>
  The Url is preserved in the browser address bar<br>
  A 404 status code is correctly set.<br>
  If I throw <code>HttpException(404, "Not Found")</code> instead of using <code>HttpNotFound()</code>, I get the 404 defined in <code>customErrors</code></p></li>
  <li><p><strong>Exceptions &amp; Unauthorised Requests</strong><br>
  Also all work correctly using this configuration of Web.config</p></li>
  </ul>
  
  <p>However, any action which checks ModelState with <code>ModelSate.IsValid</code>, or adds errors using <code>ModelState.AddModelError("InputName", "Error Message");</code> results in a 400 Bad request.</p>
  
  <p>Here are the errors I get in IE &amp; Chrome:</p>
  
  <p><strong>IE</strong><br>
  <img src="https://i.stack.imgur.com/e3GF9.jpg" alt="IE"></p>
  
  <p><strong>Chrome</strong><br>
  <img src="https://i.stack.imgur.com/GXGdy.jpg" alt="Chrome"></p>
  
  <p>Instead, what I should be getting is the original view, with the validation messages displayed next to the inputs which failed validation.</p>
  
  <p>If I remove the <code>httpErrors</code> section from web.config, the issue is resolved, but I no longer get custom IIS error pages.</p>
  
  <p>I have also tried every option of the <code>existingResponse</code> property on <code>httpErrors</code> but different options breaks either the error pages or the <code>ModelState</code> validation.</p>
  
  <p>Finally I tried setting <code>Response.TrySkipIisCustomErrors = true;</code> and this appear to fix the problem in the action I was testing.</p>
  
  <p>But this means that I have to set this property on every action which uses <code>ModelState</code>, and that just doesn't sound right.</p>
  
  <p>What am I doing wrong?</p>
  
  <p><strong>UPDATE</strong>
  To test if this was project specific, I created a new project in VS2012 and configured the same error pages. Frustratingly, everything worked.  The only difference I can think of now is that the project experiencing the problem originally started out using MVC 4, and was upgraded to MVC 5.</p>