【问题标题】:Dynamic Layout for Error View in ASP.Net MVCASP.Net MVC 中错误视图的动态布局
【发布时间】:2011-04-11 06:38:23
【问题描述】:

我有两个动作叫做“a”和“b”。我也对他们有两种看法。这些视图的布局是不同的。 对于:

@{
    Layout = "~/Views/Shared/_X.cshtml";
}

对于 b:

@{
    Layout = "~/Views/Shared/_Y.cshtml";
}

错误视图也是共享的。

如何为错误视图使用动态布局。例如,当处理动作“a”时发生错误时,错误会显示在动作“a”的布局中,如果在处理动作“b”时发生错误,错误会显示在动作“b”的布局中?

【问题讨论】:

标签: asp.net-mvc-3 razor


【解决方案1】:

你可以写一个辅助方法:

public static string GetLayout(this HtmlHelper htmlHelper)
{
    var action = htmlHelper.ViewContext.RouteData.GetRequiredString("action");
    if (string.Equals("a", action, StringComparison.OrdinalIgnoreCase))
    {
        return "~/Views/Shared/_X.cshtml";
    } 
    else if (string.Equals("b", action, StringComparison.OrdinalIgnoreCase))
    {
        return "~/Views/Shared/_Y.cshtml";
    }
    return "~/Views/Shared/_Layout.cshtml";
}

然后:

@{
    Layout = Html.GetLayout();
}

【讨论】:

    【解决方案2】:

    这个超载怎么样?

    Controller.View Method (String, String) (System.Web.Mvc)

    在一个动作中

      return View(viewName,"_X");
    

    在b动作中

      return View(viewName,"_Y";
    

    【讨论】:

      【解决方案3】:

      希望这对你有所帮助....在 Asp.Net MVC 中呈现布局的各种方式。

      方法一:使用Views文件夹根目录下的_ViewStart文件控制Layouts渲染

      我们可以在 _ViewStart 文件中使用以下代码更改布局的默认渲染:

      @{
      var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"].ToString();
      
      string layout = "";
      if (controller == "Admin")
      {
      layout = "~/Views/Shared/_AdminLayout.cshtml";
      }
      else
      {
      layout = "~/Views/Shared/_Layout.cshtml";
      }
      Layout = layout;
      }
      

      方法2:从ActionResult返回布局

      我们还可以通过使用以下代码从 ActionResult 返回布局来覆盖默认布局呈现:

      public ActionResult Index()
      {
      RegisterModel model = new RegisterModel();
      //TO DO:
      return View("Index", "_AdminLayout", model);
      }
      

      方法3:在顶部的每个视图中定义布局

      我们还可以通过使用以下代码在视图上定义布局来覆盖默认布局渲染:

      @{
      Layout = "~/Views/Shared/_AdminLayout.cshtml";
      }
      

      谢谢

      【讨论】:

        【解决方案4】:

        您可以尝试从控制器操作传递布局:在会话中设置当前布局,然后在错误控制器中检索它并通过 ViewBag 属性将其传递给视图:

        public ActionResult A()
        {
            // Do things
        
            // Set the layout
            Session["currentLayout"] = "~/Views/Shared/_X.cshtml";
        
            return View();
        }
        

        错误控制器:

        public ActionResult NotFound() // 404
        {
            // Set the layout
            ViewBag.ErrorLayout = Session["currentLayout"];
        
            return View();
        }
        

        然后在你的错误视图中:

        @{
            Layout = ViewBag.ErrorLayout;
        }
        

        我承认这不会赢得美容奖;可能还有其他方法。

        例如,看看这个关于如何在 ActionFilter 中设置布局的答案:How to set a Razor layout in MVC via an attribute filter?

        您可以编写自己的继承自 HandleError 的错误过滤器并在其中设置布局。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-04
          • 2017-05-08
          • 1970-01-01
          • 1970-01-01
          • 2021-07-29
          • 1970-01-01
          相关资源
          最近更新 更多