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):
Index.aspx:
<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="" CodeBehind="Index.aspx.cs" Inherits="MiWeb.Index" %>
<HeaderMp:Header ID="ctntHead" ContentPlaceHolderID="head" runat="server">
<title>SITPer</title>
</HeaderMp:Header>
Header.Master:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Header.Master.cs" Inherits="MasterPages.Header" %>
<!DOCTYPE html>
<html>
<head>
<title></title>
<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>
<noscript>
<link rel="stylesheet" href="css/skel.css" />
<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" href="css/style-desktop.css" />
</noscript>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<div>
<!-- Header -->
<div id="header-wrapper" >
<div id="header">
<!-- Logo -->
<div id="logo">
<h1><a href="Index.aspx">SITPer</a></h1>
<p>Prueba</p>
</div>
</div>
</div>
<asp:ContentPlaceHolder ID="body" runat="server">
</asp:ContentPlaceHolder>
</div>
</body>
</html>
Thanks!!
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.
Add -> Existing Item
.SharedFiles
folder.Header.master
, Header.master.cs
and Header.master.designer.cs
files.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...
MasterPageFile="~/Header.master"
But note that the virtual path will change if you put the file (or the linked file) in a subdirectory of your web project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With