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");
'.NET > .NET' 카테고리의 다른 글
Difference between Application_Start and Application_OnStart (0) | 2013.12.26 |
---|---|
How to include an external html file in asp.net page (0) | 2013.12.24 |
[.NET] Retrieving Data Using a DataReader (0) | 2013.11.06 |
[ASP.NET] 페이지간 데이터 전송 (0) | 2013.09.13 |
[.NET MVC] 레이저 문법 (0) | 2013.09.09 |