【问题标题】:How to handle at parent level child actions exceptions如何在父级别处理子操作异常
【发布时间】:2012-06-01 14:53:11
【问题描述】:

我目前正在我们的应用程序中添加异常管理。

在应用程序中,我的布局基本上显示了一个通过@Html.Action("news") 呈现的新闻部分,到目前为止,当发生异常时,我可以在新闻部分显示一条错误消息,说“它崩溃了”,但这并不完全是最好的吸引力...

如果子操作遇到异常,是否可以在父级别检测并重定向到错误页面?

【问题讨论】:

标签: c# asp.net-mvc asp.net-mvc-3


【解决方案1】:

这就是我所做的。这是从关于 SO 的类似问题中得到的(不确定是哪一个)。

public ActionResult TabInfo(int id, string tab)
    {
        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 e)
        {
            return View("ErrorChildAction");
        }

    }

ErrorChildAction 视图

@model System.Web.Mvc.HandleErrorInfo

@{
    Layout = null;
}

<!DOCTYPE html>
<html>
 <head>
  <title></title>
 </head>
 <body>
 <!-- Redirect to an error page in the application root -->
 <script type="text/javascript">
     window.location.href = '@Url.Content("~/400.htm")';
 </script>
</body>

HTH

【讨论】:

    【解决方案2】:

    这篇文章可以满足您的需求。在应用程序级别,它会捕获异常,您可以选择如何处理它们

    ASP.NET MVC Custom Error Handling Application_Error Global.asax?

    【讨论】:

      猜你喜欢
      • 2019-01-28
      • 1970-01-01
      • 2018-07-01
      • 1970-01-01
      • 2010-10-17
      • 2019-04-05
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      相关资源
      最近更新 更多