views:

476

answers:

1

I tried to make the ViewEngine use an additional path using:

base.MasterLocationFormats = new string[] {
    "~/Views/AddedMaster.Master"
};

in the constructor of the ViewEngine. It works well for aspx and ascx(PartialViewLocationFormats, ViewLocationFormats).

I still have to supply the MasterPage in web.config or in the page declaration. But if I do, then this declaration is used, not the one in the ViewEngine. If I use am empty MasterLocationFormats, no error is thrown. Is this not implemeted in RC1?

EDIT:

using:

return View("Index", "AddedMaster");

instead of

return View("Index");

in the Controller worked.

+1  A: 

Your example isn't really complete, but I am going to guess that your block of code exists at the class level and not inside of a constructor method. The problem with that is that the base class (WebFormViewEngine) initializes the "location format" properties in a constructor, hence overriding your declaration;

public CustomViewEngine()
{
    MasterLocationFormats = new string[] {
        "~/Views/AddedMaster.Master"
    };
}

If you want the hard-coded master to only kick in as a sort of last effort default, you can do something like this:

public CustomViewEngine()
{
    MasterLocationFormats = new List<string>(MasterLocationFormats) {
        "~/Views/AddedMaster.Master"
    }.ToArray();
}
Thomas J
I had the MasterLocationFormats set in the default constructor. Did it realy work for you?
Malcolm Frexner
Keep in mind you _must_ specify a valid MasterPage in the @Page directive in each of your views. To verify that my simple solution works, use one of the View method overloads that lets you specify a master page. E.g. "return View("Index", "ThisMasterPageDoesntExist");".
Thomas J