I have tried many method, but still fail to make it. Just want to show a 404/500 when the request is invalid.
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.
I want this behavior to apply entire apps.
public ActionResult EditUser(int Id)
{
var db = new UserDbContext();
var usr = db.Users.Where(a => a.Id == Id).SingleOrDefault();
if (usr == null) return new HttpStatusCodeResult(HttpStatusCode.NotFound, "User not found");
return View(usr);
}
with webconfig in system.web
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/General">
<error statusCode="404" redirect="~/NotFound" />
<error statusCode="403" redirect="~/BadRequest" />
</customErrors>
and in system.webserver
<httpErrors existingResponse="PassThrough"></httpErrors>
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.
Someone could please point out my mistake. Thanks