title: How to properly handle child action exceptions
body: <p>I have an Action that returns a PartialView:</p>
  
  
  
  <pre><code>[ChildActionOnly]
  public ActionResult TabInfo(int id, string tab)
  {
  
      ViewBag.Jobid = id;
      ViewBag.Tab = tab;
  
      var viewModel = _viewModelManager.GetViewModel(tab, id);
  
      return 
          PartialView(string.Format("~/Views/{0}/Index.cshtml", tab), viewModel);
  
  }
  </code></pre>
  
  <p>The <code>_viewModelManager</code> returns a view from a Dictionary. If a user requests a tab that does not exsist then a <code>KeyNotFound</code> Exception will be thrown, however, In my view I get the following Exception:</p>
  
  <p><code>Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'</code></p>
  
  <pre><code>@using MyApplication.UI.Helpers.Html
  @model MyApplication.UI.Models.MyJobModel
  
  @{
      ViewBag.Title = "Details";
  }
  
  &lt;p&gt;@Model.Blah&lt;/p&gt;
  
  ...
  
  *@ HttpException occurs here -- renders default error view *@
  @Html.Action("TabInfo", new { id = ViewBag.Jobid, tab = ViewBag.Tab }) 
  </code></pre>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.html.childactionextensions%28v=vs.98%29.aspx" rel="nofollow">According to MS...</a></p>
  
  <blockquote>
    <p>The HandleErrorAttribute attribute on a child action method is ignored if an exception occurs in the child action itself. Therefore, a child action should handle its own exceptions. If a child action has an AuthorizeAttribute attribute applied, the attribute will execute and return an HTTP Unauthorized 401 status code.</p>
  </blockquote>
  
  <p>I can't user this <code>[HandleError(ExceptionType = typeof(KeyNotFoundException), View="myError")]</code> and I can't redirect using a try/catch either because redirects for child actions are not supported.</p>
  
  <p><strong>What is the best way to handle child action exceptions?</strong></p>
  
  <p>Bottomline: I want to handle the exception and return a custom error page.</p>