Reputation: 3955
I'm writing a custom WebFormViewEngine and i would like to figure out the name of the Master Page file specified in the view's aspx file when the WebFormViewEngine.FindView method is called.
My end goal is to determine the name of the desired master page file so that i can use a master page file of the same name in a different directory to render the view.
So the challenge is that i have the file path of the view, but before the view is rendered i want to determine the name of the master page file.
I can open the file and search for MasterPageFile="%", but i'm hoping there's a better way to do this.
Thanks.
Upvotes: 0
Views: 768
Reputation: 53183
Master page lookups work differently than what you think. In MVC WebForms view engine there are basically two ways of specifying the master page:
In the <%@ Page %>
directive of the view page.
This method is not actually MVC-specific, it depends on the functionality built into WebForms control trees. You need to provide the full path and this value never gets looked at by the MVC pipeline, because it only gets set and evaluated once the view starts executing (which happens after WebFormViewEngine.FindView
).
In your action method ViewResult: return View("Index", "MyCustomMaster")
You can override the master page from your controller. In this case you can specify just the Master View name or the full path to the master file. This value does get passed into WebFormViewEngine.FindView
and it overrides whatever might be specified in the view itself.
If you always only use #2, then the values will always go through WebFormViewEngine.FindView
. However if you also use #1, then you'll basically have to do the same thing that MVC does to enable #2: write your own custom Page class:
public class MyViewPage : System.Web.Mvc.ViewPage {
protected override void OnPreInit(EventArgs e) {
base.OnPreInit(e);
// you might not need the following, but perhaps it would be useful to
// differentiate between #1 and #2
bool masterOverridenInController = !String.IsNullOrEmpty(MasterLocation);
string currentPathToMaster = MasterPageFile;
// change currentPathToMaster any way you like
MasterPageFile = currentPathToMaster;
}
}
Upvotes: 1