Correctly send user to 404 if dynamic content is not found (ASP.NET MVC)

asp.net-mvc, asp.net-mvc-3, c#, http-status-code-404

Solution

You should throw HttpException 404 :

throw new HttpException(404, "Page not Found");

Problem

I have implemented 404 handling for the general case in ASP.NET MVC 3, for when a controller/view is not found. But how should it be handled inside the controller if the user is trying to access something that can't be found? For example `www.foo.bar/Games/Details/randomjunk` will call this inside `GamesController`: ``` public ActionResult Details(string id) // id is 'randomjunk' { if(DoesGameExist(id) == false) // Now what? ``` I could just do a `return Redirect('/Errors/Http404');` but that doesn't seem like the correct way to do it. Should you throw an exception, or something else? We could have a special view in this case, but to start with we need a good way we can apply to several cases. Edit: I want to show my friendly 404 page I already have for the general case.

Original source

Related problems