Dynamically Assign Master Pages Using MVC

When using web forms, the PreInit event page appears dynamically as the appropriate place to assign master pages to a page:

this.Master.MasterPageFile = "~/leaf.Master"

If nessasary, master pages can be set here in the hierarchy of nested master pages:

this.Master.MasterPageFile = "~/leaf.Master"
this.Master.Master.MasterPageFile = "~/root.Master"

Using the MVC framework, you can dynamically set one master page name using the Viewers Controls method by passing the name masterName, but how do you set other master pages above in the hierarchy?

Update
Sorry, I was not clean.

By hierarchy, I mean a chain of nested master pages, so how can I set the topmost main page in a chain of nested master pages?

For example, we have such a setting that different types of clients have different master pages and the ones embedded in this main page are an additional main page for certain user roles. We need to dynamically install the root client wizard, as well as the role wizard.

+3
source share
2 answers

It's not entirely clear what you mean by β€œhigher in the hierarchy,” but if you mean β€œin one place, and not in every controller that I create,” I can present two options:

  • Create a supertype of abstract controller and subclass of your specific controllers.

  • Create a factory controller (subclass of DefaultControllerFactory) and override CreateController to set its own MasterPage property.

, Global.asax:

  ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());
+3

MVC .

+1

Source: https://habr.com/ru/post/1698476/


All Articles