Skip to content

Instantly share code, notes, and snippets.

@DavidWise
Created July 23, 2012 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidWise/3166582 to your computer and use it in GitHub Desktop.
Save DavidWise/3166582 to your computer and use it in GitHub Desktop.
An httpModule that allows the master pages in SharePoint to be overridden
using System;
using System.Web;
using System.Web.UI;
using System.IO;
public class MasterPageModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
}
void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
Page page = HttpContext.Current.CurrentHandler as Page;
if (page != null)
{
page.PreInit += new EventHandler(page_PreInit);
}
}
void page_PreInit(object sender, EventArgs e)
{
Page page = sender as Page;
if (page != null)
{
// Is there a master page defined?
if (page.MasterPageFile != null)
{
// only change the application.master files as those are the offenders
if (page.MasterPageFile.Contains(“application.master”))
{
page.MasterPageFile = “/_catalogs/masterpage/MyCustom.master”;
}
}
}
}
public void Dispose()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment