How to get MVC action to return 404

In ASP.NET MVC 3 and above you can return a HttpNotFoundResult from the controller.

return new HttpNotFoundResult("optional description");

There are multiple ways to do it,

  1. You are right in common aspx code it can be assigned in your specified way
  2. throw new HttpException(404, "Some description");

In MVC 4 and above you can use the built-in HttpNotFound helper methods:

if (notWhatIExpected)
{
    return HttpNotFound();
}

or

if (notWhatIExpected)
{
    return HttpNotFound("I did not find message goes here");
}