Whats the earliest point I can programatically get to the name of the masterpage that will be used in an asp.net MVC application?
我能编程的最早的点是什么?它将被应用于asp.net MVC应用程序中。
The idea being changing the viewdata used depending on the master page. I want to be able to set something as early as possible so that individual developers don't need to know to populate it. Ideally it would just happen in the background.
其思想是根据母版页更改所使用的视图数据。我希望能够尽可能早地设置一些东西,这样单个开发人员就不需要知道如何填充它。理想情况下,它只会发生在后台。
In the simplist scenario we would have different masterpages for different types of page, such as a default one with a header, leftnav, rightnav and footer or a secure one which could be missing the rightnav.
在simplist场景中,对于不同类型的页面,我们会有不同的主页面,比如默认的页眉、左侧导航、右导航和页脚,或者一个可能会丢失右导航的安全页面。
Thanks
谢谢
1 个解决方案
#1
2
I'd say the point when your master page is known would be right after your view has been resolved by your view engine. You can set your masterpage when calling the View method on your Controller though:
我想说的是,当您的主页被您的视图引擎解析后,您的主页就会被知道。当调用控制器上的视图方法时,您可以设置主页:
public ActionResult Index()
{
return View("index","masterpagename");
}
I think if you want to do anything with your masterpage THAT would be the way. I don't know the context of your question, but I assume you want to manipulate the view and/or viewdata depending on which masterpage is used? Maybe the solution to your problem is not programmatically getting the name of the masterpage in the first place?
我想如果你想用你的主页做任何事,那就这样。我不知道您的问题的上下文,但我假设您希望根据使用哪个母版来操作视图和/或视图数据?也许您的问题的解决方案不是首先以编程方式获取母版的名称?
Addition, after your edit: What you could do is create a new 'Base' controller which inherits the default Controller class. Then you override the View methods, and tell your developers to use that:
另外,在编辑之后:可以创建一个新的“Base”控制器,它继承默认的controller类。然后重写视图方法,并告诉开发人员使用:
public class MySuperController : Controller
{
protected override ViewResult View(string viewName, string masterName, object model)
{
// do something here so your masterpage is different depending on the context //
string newMasterName = "something-something";
return base.View(viewName, newMasterName, model);
}
}
I 'think' all other view methods either get routed through this, or are not used in your case (the ones requiring you to pass an IView).
我“认为”所有其他视图方法要么通过这个路由,要么在您的案例中不使用(那些需要您传递IView的方法)。
#1
2
I'd say the point when your master page is known would be right after your view has been resolved by your view engine. You can set your masterpage when calling the View method on your Controller though:
我想说的是,当您的主页被您的视图引擎解析后,您的主页就会被知道。当调用控制器上的视图方法时,您可以设置主页:
public ActionResult Index()
{
return View("index","masterpagename");
}
I think if you want to do anything with your masterpage THAT would be the way. I don't know the context of your question, but I assume you want to manipulate the view and/or viewdata depending on which masterpage is used? Maybe the solution to your problem is not programmatically getting the name of the masterpage in the first place?
我想如果你想用你的主页做任何事,那就这样。我不知道您的问题的上下文,但我假设您希望根据使用哪个母版来操作视图和/或视图数据?也许您的问题的解决方案不是首先以编程方式获取母版的名称?
Addition, after your edit: What you could do is create a new 'Base' controller which inherits the default Controller class. Then you override the View methods, and tell your developers to use that:
另外,在编辑之后:可以创建一个新的“Base”控制器,它继承默认的controller类。然后重写视图方法,并告诉开发人员使用:
public class MySuperController : Controller
{
protected override ViewResult View(string viewName, string masterName, object model)
{
// do something here so your masterpage is different depending on the context //
string newMasterName = "something-something";
return base.View(viewName, newMasterName, model);
}
}
I 'think' all other view methods either get routed through this, or are not used in your case (the ones requiring you to pass an IView).
我“认为”所有其他视图方法要么通过这个路由,要么在您的案例中不使用(那些需要您传递IView的方法)。