I'm fairly new to ASP.NET MVC and am not sure how best to handle the following situation.
A method in my controller needs to load some data based on an ID argument. Under normal circumstances, this ID argument will be set to a valid ID of an entity within my database. I construct some data and place it in ViewBag, which the view uses to render the page.
However, I would like some basic error handling just in case the ID argument is not valid. Although I could write a bunch of error handling code in the view, it would be much simpler not to display the view if there is a major misuse or malfunction of the site.
Is there a way the controller could simply return a "Item not found" string or something like that, and display that rather than the normal view? Or perhaps someone can suggest a better idea?
======================================================================
if (itemId == null)
{
return Content("Item not found");
}
Or if you want to return an HTTP 404 instead:
throw new HttpException(404, "Item Not Found");