ASP.NET MVC Tip – Return specific views for specific errors.
Earlier I had said to keep your controllers as thin as possible, that doesn't mean that they should necessarily just be two, or one, lines of code.
Take an instance where you are retrieving items from a web service in your controller. Let's say that you get a 404 error and your service will throw an exception telling you that the service can't be found. Take a look at this controller action
-
public ViewResult Item(string itemID)
-
{
-
Item item = MyWebServiceWrapper.GetItem(itemID);
-
return View("Item", item);
-
}
As it stands this action is pretty thin, but if an exception is thrown the user will be presented with either whatever custom error page you have defined in your web.config or the dreaded Yellow Screen of Death.(YSOD). You can wrap the call in a try-catch block and catch the exception. But rather than creating a generic errors page that will display any raw exception message, it's a much better idea to return an error specific view to your user with a friendly error message.
-
public ViewResult Item(string itemID)
-
{
-
try
-
{
-
Item item = MyWebServiceWrapper.GetItem(itemID);
-
return View("Item", item);
-
}
-
catch(ServiceCannotBeReachedException)
-
{
-
Item item = new Item(itemID);
-
return View("CannotRetrieveItem", item);
-
}
-
}
-
-
//The CannotReachService view has a line that looks like this
-
// "The item <%= Model.itemID %> can not be retrieved at this time. Please try again later"
-
// obviously this can be globalized/localized as needed.
This leads to a much "dumber" view and is a much better idea than modifying the "Item" view to display errors if something isn't right with the model or passing in special error parameters to the view. Let the view deal with the data it is meant to display and nothing else.
-
http://pabloblamirez.blogspot.com PaulBlamire
-
http://flux88.com Ben Scheirman
-
http://pinnaclesecurity.com/security-systems.php security system


