Global Master Pages in ASP.NET

19. September 2007

I recently was working on a project and decided it would have been excellent to have global master pages so I can keep my web projects independent from each other, but keep the same look.  In this blog I will show you the steps I have taken to create a website that utilizes global master pages in ASP.NET.  I would like to say I got the idea on how to do this via the following article: http://odetocode.com/Articles/450.aspx

I am going to use the concept of storing the master pages in a directory on the file system and having my sub projects create a virtual directory so they can reference the master page to use. 

In the near future I will be writing an entire blog split up into parts on how to create an application that incorporates global master pages, web parts with the membership provider, themes, and keeping the web project independent from each other.

So let’s begin on how to create and use global master pages.

1. Create a new web project in Visual Studio
2. Remove all default folders and files except the web.config
3. Create a new master page(s)
4. Design and layout the master page(s) and needed.
      Note: do not use any code behind files otherwise you will need to copy these dll’s into each bin directory of your sub projects.  If someone has found a way to avoid this please comment on this article to help out.

There is no need to publish or pre-compile this website because there isn’t any code behind files so the code will be compiled by the engine once the page is visited.

5. Once the project is saved copy it somewhere on the file system (i.e., D:\GlobalMasterPages)

6. Now create a new web project and leave the default configuration
7. Go into the IIS Manager and create a new virtual directory inside your new website that points to the path where you copied the global master page project

Refresh the solution in Visual Studio and you will see the virtual directory inside there. 

8. Now, in the default.aspx page reference the master page in the markup:  Below is an example of what it may look like:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="_default" MasterPageFile="~/ GlobalMasterPages/site.master" %>

9. Next, you need to remove the normal html and body tags so you can add the <asp:Content> tag to the file.  When adding the <asp:Content> tag you need to give it the same contentPlaceHolderID as you normally would as if that master page was truly inside your project.  For example if one of your content place holder names was plcHolderMiddle your page should look something like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="_default" MasterPageFile="~/ GlobalMasterPages/site.master" %>

<asp:Content ID="Content2" ContentPlaceHolderID="plcHolderMiddle" Runat="Server">
 
 code…
 code..
 code..
</asp:Content>


You should now have design view capability for that content place holder if you wish.  You will not see the rest of the master page, just the content place holders you are using.

Now you can run this web project and you should see the master page you created earlier that is not even stored inside this web project. 

You can use this method for as many web projects as you wish.  As I mentioned in the beginning of this blog, I will be writing a full series on how to truly utilize this concept for a full blown web application.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

ASP.NET ,

blog comments powered by Disqus