Monday, April 6, 2009

Sharing Master Pages across web Applications in ASP.Net

The MasterPage class available in ASP.NET 2.0 derives from UserControl and just like user controls, master pages can't be shared across IIS applications. There are a few different solutions that have been proposed, such as setting up virtual directories in each IIS application that point to the same physical folder, but there is a way to share master pages across applications with a little work on your part without resorting to duplicating virtual directories across multiple websites. By leveraging the VS.NET 2005 Publish Web Site tool it's possible to create an assembly that contains all of the master page HTML code and C# or VB.NET code, give the assembly a strong name, and install it into the Global Assembly Cache (GAC).
There are several steps involved, so a step-by-step approach follows, and also issues to watch out for when performing these steps.

- Create an empty Website in VS.NET 2005. Delete everything in it including
App_Data, Default.aspx, and web.config (if it exists).
- Add a master page into the website

<%@ Master Language="C#" %>

<html xmlns="" >
<head runat="server">
<title>Untitled Page </title>
<form id="form1" runat="server">

Shared Master Header

<br />

<br />

Shared Master Footer


- Publish this Website

- On the screen that follows, select a target location, and check all of the
checkboxes shown in Figure Note that the image shown in Figure references a strong
name key file named keyfile.snk that was created using the sn.exe command-line
tool that ships with .NET. This is required in order to install assemblies into
the GAC. The following syntax can be used to create the key file (run it using the
Visual Studio .NET 2005 command prompt): sn.exe -k keyfile.snk.

- After the publish operation completes, open the new website in VS.NET 2005 and
add a reference to the newly created assemly in the website

- Install the assembly into the GAC using gacutil.exe or drag-and-drop it into
c:\Windows\Assembly using Windows Explorer.

Once you've done this, delete the original assembly as well as the newly created
XML files associated with it from the website.

- Add a web.config file into the website and add the following within the
<system.web> begin and end tags.
<compilation debug="true">
<add assembly="App_Web_masterpagebase.master.cdcab7d2, Version=,
Culture=neutral, PublicKeyToken=cceb8435cfc68486" />

You'll need to change the name of the assembly to the name that is generated for your project (the one you added into the GAC) and change the PublicKeyToken to the one you see in the GAC.

- Add a master page into the website, but don't create a code-behind page for it(you
can, but it's not needed in this case since the master page is only used to
reference the one installed in the GAC).

- Remove all code within the new master page and add the following at the top. It
should be the only code in the page.
<%@ Master Language="C#" Inherits="ASP.masterpagebase_master" %>
If you named the original master page (the one created in step 2)
differently,then you'll need to change the Inherits value. Use the VS.NET object
browser to see the name of the class within the .dll generated in step 4.

- Create a content page that references MasterPage.master. The default
ContentPlaceHolderID is ContentPlaceHolder1, so use that in the tag
unless you gave the id a different name in step 2.

After completing these steps, any web application can share the same master page used by other web applications by placing the empty MasterPage.master file into the application and updating the web.config file to point to the master page assembly in the GAC.

The downfall of this approach is that you have to recompile the base master page and put it back into the GAC each time you need to make a change and the design time support is lacking.

If you don’t want to install the assembly into GAC then follow a very simple approach as follows-

Publish the base Masterpage website without SN key option, and refer the published assembly in your new web site.

Now whenever you want to change the base master page republish the base master page website and just rebuild your new website which is referencing base master page.

Important: Take care of the names referred in INHERITS section of referring master page’s <%@ Master Language="C#" nherits="ASP.masterpagebase_master" %>, since that is an important thing which can cause errors in your website.

Thanks to Dan Wahlin (Ref.) and his blog

if this was helpful then please donate as much you want