title: ASP.NET MVC showing error page on when sending responses with status 500
body: <p>I have the following action in the server:</p>
  
  <pre><code>[HttpPost]
  public JsonResult SearchContracts(SearchViewModel vm)
  {
      List&lt;string&gt; errors;
  
      if (IsValid(vm, out errors))
      {
          return Json(service.Search(vm), JsonRequestBehavior.AllowGet);
      }
      else
      {
          HttpContext.Response.StatusCode = 500;
  
          return Json(new { Errors = errors }, JsonRequestBehavior.AllowGet);
      }
  }
  </code></pre>
  
  <p>Locally, it works great. When the request is not valid, it returns a JSON with the errors and the response has a <code>500</code> for the HTTP status code.</p>
  
  <p>When deployed, instead of the JSON described, IIS is returning me <a href="http://codepen.io/nescalante/full/pvNZoX/" rel="nofollow noreferrer">this famed error page</a>.</p>
  
  <p>Here is the <code>web.config</code> with the detail for the <code>customErrors</code> section:</p>
  
  <pre><code>&lt;customErrors mode="Off" defaultRedirect="~/error.htm"&gt;
      &lt;error statusCode="404" redirect="~/errorhandling/pagenotfound" /&gt;
  &lt;/customErrors&gt;
  </code></pre>
  
  <p>I tried turning it to <code>On</code> but neither worked.</p>
  
  <p>Where should I change to stop recieving that ugly error page instead of my beauty JSON?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I changed the status code to <code>400</code>, now I am getting the text <code>Bad Request</code> as a response, instead of the JSON:</p>
  
  <p><img src="https://i.stack.imgur.com/I5oVT.png" alt="Bad request text"></p>