Is it possible to alter the "MasterPageFile" from an extension?

Topics: Business Logic Layer
May 14, 2013 at 6:29 PM
Edited May 14, 2013 at 6:31 PM
Hello;

I wanted to extend the themes capability of BlogEngine.Net to give more control over the format of the Home Page, the content pages and the blog pages. I have found that in my designs these page types carry a common theme but are unique enough to where I wanted them to have their own sub themes (.master/css/art).

I accomplished this by modifying the BlogBasePage.css - OnPreIni
            string pageType = Page.GetType().ToString();

            switch (pageType)
            {
                case "ASP.contactus_aspx":

                    MasterPageFile = string.Format("{0}themes/{1}/SitePage.master", Utils.ApplicationRelativeWebRoot, BlogSettings.Instance.GetThemeWithAdjustments(null));
                    break;

                case "ASP.page_aspx":

                    Guid id = new Guid(Request.QueryString["id"]);
                    BlogEngine.Core.Page p = BlogEngine.Core.Page.GetPage(id);

                    bool isFrontPage = p.IsFrontPage;

                    if (isFrontPage)
                    {
                        MasterPageFile = string.Format("{0}themes/{1}/SitePageHome.master", Utils.ApplicationRelativeWebRoot, BlogSettings.Instance.GetThemeWithAdjustments(null));
                    }
                    else
                    {
                        MasterPageFile = string.Format("{0}themes/{1}/SitePage.master", Utils.ApplicationRelativeWebRoot, BlogSettings.Instance.GetThemeWithAdjustments(null));
                    }

                    break;
               
                default:

                    MasterPageFile = string.Format("{0}themes/{1}/SiteBlog.master", Utils.ApplicationRelativeWebRoot, BlogSettings.Instance.GetThemeWithAdjustments(null));
                    break;
            }
This works great, but I wanted to see if there was a way to implement this change through an extension rather the messing with the base code.

Any thoughts? (Is it possible?)

Thanks
Coordinator
May 15, 2013 at 1:28 AM
Edited May 15, 2013 at 1:50 AM
I guess we could expose property in the base class to allow get/set master page, need to evaluate this closer though.

Actually, you can only change master page before page init, so cleaner would be do it in default.aspx.cs (or any page inherited from blog base page, like contact.aspx.cs).
No need to mess with core DLL, just add something like:
protected void Page_PreInit(object sender, EventArgs e)
{
    if (Request.QueryString["foo"] == "1")
        this.Page.MasterPageFile = "~/themes/Indigo/site.master";
    else
        this.Page.MasterPageFile = "~/themes/Standard/site.master";
}
May 16, 2013 at 7:28 PM
Thanks for the update, I'll give this a try ... much cleaner solutions :)