我有一个返回 PartialView 的操作:
[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);
}
从 Dictionary中_viewModelManager
返回一个视图。如果用户请求一个不存在的选项卡,KeyNotFound
则会引发异常,但是,在我看来,我得到以下异常:
Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'
@using MyApplication.UI.Helpers.Html
@model MyApplication.UI.Models.MyJobModel
@{
ViewBag.Title = "Details";
}
<p>@Model.Blah</p>
...
*@ HttpException occurs here -- renders default error view *@
@Html.Action("TabInfo", new { id = ViewBag.Jobid, tab = ViewBag.Tab })
如果子操作本身发生异常,则忽略子操作方法上的 HandleErrorAttribute 属性。因此,子动作应该处理自己的异常。如果子操作应用了 AuthorizeAttribute 属性,则该属性将执行并返回 HTTP Unauthorized 401 状态代码。
我不能使用它[HandleError(ExceptionType = typeof(KeyNotFoundException), View="myError")]
,也不能使用 try/catch 重定向,因为不支持子操作的重定向。
处理子操作异常的最佳方法是什么?
底线:我想处理异常并返回自定义错误页面。