tags:

views:

559

answers:

3

When using webforms the appropriate place to assign master pages to a page dynamically seems to be the pages PreInit event:

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

If nessasary, master pages in a hierarchy of nested master pages may be set here too:

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

Using the MVC framework you can set a single master page name dynamically using the controllers View method by passing the masterName, but how do you set other master pages higher up in the hierarchy?

Update
Sorry I was not clear.

By hierarchy i mean a chain of nested master pages, so how can i set the very top master page in a chain of nested master pages?

For example we have a set up such that different customer types have different master pages and nested within this master page is an additional master page for specific user roles. We need to dynamically set the root customer master as well as the role master.

+2  A: 

It isn't entirely clear what you mean by "higher up in the hierarchy," but if you mean, "in one place, rather than in every controller I create," I can think of two options:

  1. Create an abstract controller supertype and subclass your concrete controllers from that.

  2. Create a controller factory (subclass DefaultControllerFactory), and override CreateController to set a custom MasterPage property.

If you choose the latter solution, you need to do this in Global.asax:

  ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());
Craig Stuntz
Controllers don't set master pages directly, as they never have direct access to the view.
Brad Wilson
I know. But read his question in full, where he explains what he wants to do.
Craig Stuntz
I think you missed my point. You're talking about Controllers, as though Controllers ever have access to the actual view. They do not. Your solution doesn't actually work.
Brad Wilson
Brad, again, I understand your point, but read his message in full, particularly the code samples (Controller has no Master property; ViewPage does). He wants to pass a master name to the view via ViewData and use it in the View, not set a property on the view directly.
Craig Stuntz
A: 

Sorry I was not clear.

By hierarchy i mean a chain of nested master pages, so how can i set the very top master page in a chain of nested master pages?

For example we have a set up such that different customer types have different master pages and nested within this master page is an additional master page for specific user roles. We need to dynamically set the root customer master as well as the role master.

PhilHoy
U might want to add this as a comment to the main entry instead of as an answer.
Adron
+1  A: 

There is no facility for this in MVC today.

Brad Wilson