Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I 'm trying to create a HttpModule that allows to route to different MasterPage preInt event. I' not an Expert about MasterPages. I'm trying to nderstand about MasterPages Token. I knows there are three type: custom and Default. In this tutorial I found this code :

void page_PreInit(object sender, EventArgs e)
    {
        Page page = sender as Page;
        string pageNo = ConfigurationManager.AppSettings["MasterPageNo"];




        if (page != null)
        {
            if (pageNo.Equals("1"))
            {
                page.MasterPageFile = "~masterurl/custom.master";
                if (SPContext.Current != null)
                {
                    SPContext.Current.Web.CustomMasterUrl = "/_catalogs/masterpage/custom1.master";
                }
            }
            else if (pageNo.Equals("2"))
            {
                page.MasterPageFile = "~masterurl/custom.master";
                if (SPContext.Current != null)
                {
                    SPContext.Current.Web.CustomMasterUrl = "/_catalogs/masterpage/custom2.master";
                }


            }
            else
            {
                page.MasterPageFile = "~masterurl/default.master";
                if (SPContext.Current != null)
                {
                    SPContext.Current.Web.MasterUrl = "/_catalogs/masterpage/default.master";
                }
            }


        } 

In Sharepoint , I have three MasterPages :

-Default.Master -GeneraliTeamSite.MasterPage.Master -GeneraliTeamSiteWiki.MasterPage.Master.master

With Sp Designer, I have set Default.master as Default, GeneraliTeamSite.MasterPage.Master as Custom. Now if i Run the page, I got this exception :

The file '/_layouts/~masterurl/custom.master' does not exist.   at System.Web.UI.Util.CheckVirtualFileExists(VirtualPath virtualPath) 
   at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile) 
   at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile) 
   at System.Web.Compilation.BuildManager.GetVPathBuildResult(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile) 
   at System.Web.UI.MasterPage.CreateMaster(TemplateControl owner, HttpContext context, VirtualPath masterPageFile, IDictionary contentTemplateCollection) 
   at System.Web.UI.Page.get_Master() 
   at System.Web.UI.Page.ApplyMasterPage() 
   at System.Web.UI.Page.PerformPreInit() 
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
share|improve this question

2 Answers 2

Basically, the code looks very good, although i noticed one error:

Here is how to set the new masterpage:

page.MasterPageFile="~/_catalogs/masterpage/v4_minimal.master"

Because what you did is:

page.MasterPageFile = "~masterurl/custom.master";

And this will not work due to a wrong path ...

Here is the full example, but check for the correct path ...

if (page.MasterPageFile != null)
{
    page.MasterPageFile = "~/_catalogs/masterpage/custom.master";
    if (pageNo.Equals("1"))
    {
        if (SPContext.Current != null)
        {
            SPContext.Current.Web.CustomMasterUrl = "/_catalogs/masterpage/custom1.master";
        }
    }
    else if (pageNo.Equals("2"))
    {
        if (SPContext.Current != null)
        {
            SPContext.Current.Web.CustomMasterUrl = "/_catalogs/masterpage/custom2.master";
        }
    }
    else
    {
        page.MasterPageFile = "~/_catalogs/masterpage/default.master";
        if (SPContext.Current != null)
        {
            SPContext.Current.Web.MasterUrl = "/_catalogs/masterpage/default.master";
        }
    }
}

Hope it was usefull!

Happy coding!

share|improve this answer
    
Unfortunately it still not working. MEssage error is : "The file /_catalogs/masterpage/custom.master does not exist. " Do you know how should I do to debugg attaching to iis? I do this well but breakpoint doesn't hit!!! –  Bruno Ferreira Nov 21 '12 at 13:52

This is some code I've used in the past to dynamically set the masterpage based on a querystring in the URL.

protected override void OnPreInit(EventArgs e) {
    base.OnPreInit(e);
    if (this.Request.Url.AbsoluteUri.Contains("frame=true")) {
        this.MasterPageFile = "framed.master";
    }
}

http://mike-greene.com/2011/10/dynamic-master-page-logic/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.