How to share Master Pages between my projects

Posted on

Problem :

I’m working on a Web Project and I need to share one master page so that when I create a new page, it has the basic layout.
What i’ve thought is to create a project with that master page and, in each web project, add it as a reference.
But the problem is that i don’t know how to embed the master page in the .aspx file that I want to apply the MP, or if it’s the best way of sharing master pages between projects.
I’ll apreciate any help or comment!

Here I give you my code (this doesn’t work):


<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="" CodeBehind="Index.aspx.cs" Inherits="MiWeb.Index" %>
    <HeaderMp:Header ID="ctntHead" ContentPlaceHolderID="head" runat="server">


<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Header.Master.cs" Inherits="MasterPages.Header" %>

<!DOCTYPE html>

    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <meta name="description" content="" />
        <meta name="keywords" content="" />
        <script src="js/jquery.min.js"></script>
        <script src="js/jquery.dropotron.min.js"></script>
        <script src="js/skel.min.js"></script>
        <script src="js/skel-layers.min.js"></script>
        <script src="js/init.js"></script>         
            <link rel="stylesheet" href="css/skel.css" />
            <link rel="stylesheet" href="css/style.css" />
            <link rel="stylesheet" href="css/style-desktop.css" />
    <asp:ContentPlaceHolder ID="head" runat="server">
        <!-- Header -->
        <div id="header-wrapper" >
            <div id="header">                   
                <!-- Logo -->
                <div id="logo">
                    <h1><a href="Index.aspx">SITPer</a></h1>
        <asp:ContentPlaceHolder ID="body" runat="server">



Solution :

NOTE: In addition to the linked files feature described here, there is a new shared project feature in Visual Studio 2015+ that allows you to create a project exclusively for sharing code that will be compiled into the project it is added to.

Master pages can only belong to a single web project. However, there is a feature of Visual Studio/MSBuild that allows you to share the code between projects to effectively get the desired result. If you put the master page files (all of them including .master, .master.cs and .master.designer.cs) into a folder at the same level as your solution you can use the linked file feature of Visual Studio to add the file as a linked file (one that is not copied into the project folder).

In Windows Explorer, open up the directory that has your solution (.sln) file. Right click in the whitespace and click New -> Folder. Name the folder SharedFiles. Copy the Header.master, Header.master.cs and Header.master.designer.cs files into the folder.

In Visual Studio, delete the Header.master file(s) from all of the projects. Then follow these steps for each project you want to share the Header.master page in.

  1. Right-click on the project you wish to share the file in Solution Explorer, and click Add -> Existing Item.
  2. In the Add Existing Item dialog, navigate to the SharedFiles folder.
  3. Highlight the Header.master, Header.master.cs and Header.master.designer.cs files.
  4. In the bottom right corner of the Add Existing Item dialog, click the down arrow next to the Add button (not the Add button itself!), and click the Add As Link item in the dropdown.

If you did it correctly, the icon of the file will have an arrow on it to indicate that it is a linked file. Now when you make changes to the master page, the changes will be reflected in all of the projects.

You also have an issue that you must fix in your Index.aspx page. You must set the master page file to the virtual location of the file. If you follow the instructions above, the path will be…


But note that the virtual path will change if you put the file (or the linked file) in a subdirectory of your web project.

i think you are not adding the address of your masterpage in your page (MasterPageFile=””).
are you using visual studio?
if you are then you just need to use
ctrl+shift+a and select web form using masterpage.

Master Page:
    <%@ Master Language="VB" AutoEventWireup="false" CodeBehind="Site1.master.vb" Inherits="WebApplication2.Site1" %>

        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">


    Page.aspx which uses masterpage:
        <%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site1.Master" CodeBehind="Registration Form.aspx.vb" Inherits="WebApplication2.Registration_Form" %>
        <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

Leave a Reply

Your email address will not be published. Required fields are marked *