代码之家  ›  专栏  ›  技术社区  ›  balexandre

如何在MVC2下以编程方式设置母版页

  •  3
  • balexandre  · 技术社区  · 14 年前

    在普通的ASP.NET中,这是一项简单的工作。。。只需覆盖 Page_PreInit 完成了!

    但我怎么才能在 ASP.NET MVC2 ?

    2 回复  |  直到 14 年前
        1
  •  5
  •   Omar    14 年前

    在ASP.NET MVC中更容易,只需将母版页的名称作为第二个参数传入即可:

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

    当然,你也可以创建自己的 System.Web.Mvc.ViewPage 然后在那里更改母版页。

        2
  •  2
  •   jim tollan    14 年前

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

    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);
    }
    

    另一种上吊的方法,我是说,剥猫皮:)