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

    Hi,

    Have you tried using the HandleError attribute for redirecting to a custom view on a particular type of error?

    [HandleError(View="CannotRetrieveItem",ExceptionType=typeof(ServiceCannotBeReachedException))]

    Kind regards,
    Paul

  • http://flux88.com Ben Scheirman

    +1 to what paul said. This keeps your actions tighter and you can reuse this logic across multiple actions/controllers.

  • http://www.lazycoder.com Scott

    Paul,Ben – I thought about HandleError, maybe I should update the post. In most cases, I use very specific exceptions that really only apply to a single controller or action. But I can see the point about reusing the logic in multiple places.

    What do the tests for actions with this attribute look like? Do you just call the action and catch the exception?

  • http://pinnaclesecurity.com/security-systems.php security system

    Good thing I came across this article. I agree it’s a much better idea to return an error specific view to your user with a friendly error message