EDIT: Apparently, for Darin Dimitrov , what I had before does not work even with customErrors. As Antonio Bakula said in another answer, you should do:
throw new HttpException(404, "Not found")
Then customErrors will be executed.
There, the built-in helper method is called HttpNotFound, so you can just do:
return HttpNotFound (); Strike>
You can also explicitly return 404 with HttpStatusCodeResult:
return a new HttpStatusCodeResult (404); Strike>
HttpStatusCodeResultUseful if you don’t need a specific helper method or class for the code you want. Strike>
You should also have this in your Web.config:
<customErrors>
<error statusCode="404" redirect="~/Custom404Page.html"/>
</customErrors>
defaultRedirect.