MVC Simple Error Handle BadRequest
<p>I have tried many method, but still fail to make it.
Just want to show a 404/500 when the request is invalid.</p>

<p>Eg. There have no User Id = 100, if someone make this request, it should return 404. On the other hand, Throw 500, if server error happened.</p>

<p>I want this behavior to apply entire apps.</p>

<pre><code>public ActionResult EditUser(int Id)
    {
        var db = new UserDbContext();
        var usr = db.Users.Where(a =&gt; a.Id == Id).SingleOrDefault();
        if (usr == null) return new HttpStatusCodeResult(HttpStatusCode.NotFound, "User not found");
        return View(usr);
    }
</code></pre>

<p>with webconfig in system.web</p>

<pre><code>&lt;customErrors mode="On"  redirectMode="ResponseRewrite" defaultRedirect="~/General"&gt;
  &lt;error statusCode="404" redirect="~/NotFound" /&gt;
  &lt;error statusCode="403" redirect="~/BadRequest" /&gt;
&lt;/customErrors&gt;
</code></pre>

<p>and in system.webserver</p>

<pre><code>&lt;httpErrors existingResponse="PassThrough"&gt;&lt;/httpErrors&gt;
</code></pre>

<p>Tried many combination way, cannot make it work.
The result i get is a empty page, then browser console showing the 404/500 error. But I want it to show on page. </p>

<p>Someone could please point out my mistake. Thanks</p>