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="http://www.w3.org/1999/xhtml" >
<title>Untitled Page </title>
<form id="form1" runat="server">
Shared Master Header
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.
<add assembly="App_Web_masterpagebase.master.cdcab7d2, Version=0.0.0.0,
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
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