Search code examples
asp.net-mvcviewengine

MasterLocationFormats in WebFormViewEngine not used?


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.


Solution

  • 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();
    }