如何妥善处理孩子的异常行为 [英] How to properly handle child action exceptions

查看:104
本文介绍了如何妥善处理孩子的异常行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回PartialView的动作:

I have an Action that returns a 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);

}

_viewModelManager 返回从字典视图。如果一个用户请求不那么exsist一个 KeyNotFound 会抛出异常,标签然而,在我看来,我得到了以下异常:

The _viewModelManager returns a view from a Dictionary. If a user requests a tab that does not exsist then a KeyNotFound Exception will be thrown, however, In my view I get the following Exception:

执行的处理子请求错误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 }) 

<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.html.childactionextensions%28v=vs.98%29.aspx\"相对=nofollow>据MS ...

这是一个孩子的动作方法的HandleErrorAttribute属性,如果在孩子行动本身发生异常会被忽略。因此,孩子应该采取行动处理自己的异常。如果一个孩子行动的AuthorizeAttribute应用属性,该属性将执行,并返回一个HTTP 401未经授权状态code。

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.

我不能使用的此 [的HandleError(ExceptionType = typeof运算(KeyNotFoundException),查看=myError)] 和使用的try / catch我不能重定向要么是因为孩子的行为重定向不被支持。

I can't user this [HandleError(ExceptionType = typeof(KeyNotFoundException), View="myError")] and I can't redirect using a try/catch either because redirects for child actions are not supported.

什么是处理孩子的行动异常的最好方法?

底线:我要处理的异常,并返回一个自定义错误页

Bottomline: I want to handle the exception and return a custom error page.

推荐答案

在情况下,任何人都遇到这个问题。

In case anyone else comes across this question.

我结束了使用try / catch块捕捉KeyNotFound例外。我记录错误,然后将用户重定向到一个ErrorView。在错误观点我将用户重定向到相应的视图使用JavaScript。

I ended up using a try/catch block to catch the KeyNotFound exception. I log the error and then redirect the user to an ErrorView. In the error view I redirect the user to the appropriate view using javascript.

[ChildActionOnly]
    public ActionResult TabInfo(int id, string tab, string jobno)
    {
        try
        {
            var viewModel = _viewModelManager.GetViewModel(tab, id);

            ViewBag.Jobid = id;
            ViewBag.Tab = tab;

            return PartialView(string.Format("~/Views/{0}/Index.cshtml", tab), viewModel);
        }
        catch (Exception ex)
        {
            return View("Error");
        }

    }

错误查看

@model System.Web.Mvc.HandleErrorInfo

@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <script type="text/javascript">
        window.location.href = '@Url.Content("~/400.htm")';
    </script>
</body>
</html>

这篇关于如何妥善处理孩子的异常行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆