views:

263

answers:

1

Whats the earliest point I can programatically get to the name of the masterpage that will be used in an asp.net MVC application?

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.

Thanks

+2  A: 

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:

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).

Erik van Brakel
Yes, that's correct, essentially changing the view data depending on the master page used.
DeletedAccount
Do you have a simplified case you could add to your opening post? I can't really see the reason for doing this?
Erik van Brakel
I've updated my post, hope that makes it clearer :o)
DeletedAccount
Cool... looks like that'll do the job - thanks!
DeletedAccount