【问题标题】:How can I set the Master Page programmatically under MVC2如何在 MVC2 下以编程方式设置母版页
【发布时间】:2010-11-17 20:14:48
【问题描述】:

在普通的 ASP.NET 中,这很容易......只需覆盖 Page_PreInit 并完成!

但我如何在 ASP.NET MVC2 下完成此操作?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-2 master-pages


    【解决方案1】:

    在 ASP.NET MVC 中更简单,只需传入母版页的名称作为第二个参数:

    return View("MyView", "MyMasterPage");
    

    当然,您也可以创建自己的System.Web.Mvc.ViewPage 并在其中更改母版页。

    【讨论】:

      【解决方案2】:

      如果您有一些在管理员和用户站点之间共享的操作,您也可以在覆盖中执行此操作(在控制器或基本控制器中):

      protected override ViewResult View(string viewName, 
                                         string masterName, object model)
      {
      
          // we share some views that aren't partialviews
          // therefore, we have to ensure that the Shareholder master template
          // is ALWAYS attached to the logged in user if they aren't an admin user
          bool userIsAdmin = IsAuthorised("Admin");
      
          if (!userIsAdmin) // then flip the masterpage to Shareholder.Master
          {
              masterName = "Shareholder";
          }
      
          return base.View(viewName, masterName, model);
      }
      

      另一种上吊方式,我的意思是,给猫剥皮 :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-23
        相关资源
        最近更新 更多