Monday, April 16, 2012

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

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.





No comments:

Post a Comment