Suppose you have two seperate ASP.NET Web Application projects that both need to use a common MasterPage.
What’s the best way to share the MasterPage across projects without having to duplicate code? Preferably without having to resort to source control or file system hacks.
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
I have trying to accomplish the same thing. I look into a couple of solutions but I think using a virtual directory is probably the best way to share master pages.
Here are a couple sources that you can look at.
- Sharing Master Pages amongst
Applications by Embedding it in a
- Sharing Master Pages in Visual Studio
- ASP.Net 2.0 – Master Pages: Tips, Tricks, and Traps
The third bullets near the end the article tells you of possible ways you can share Masterpages also.
From K. Scott Allen’s ASP.Net Master Pages: Tips, Tricks, and Traps article, on “Sharing Master Pages”:
The first alternative is to copy shared master page files into a
single location on an IIS web server. Each application can then create
a virtual directory as a subdirectory and point the virtual directory
to the real directory of master pages. The applications can then set
the MasterPageFile property of a page to the name of the virtual
directory, plus the name of the master page file. When we drop an
updated master page file into the real directory, the new master page
will appear in all the applications immediately.
A second approach is to use a version control system to share a set of
master page files across multiple projects. Most source control /
version control systems support some level of “share” functionality,
where a file or folder can appear in more than one project. When a
developer checks in an updated master page file, the other projects
will see the change immediately (although this behavior is generally
configurable). In production and test, each application would need to
be redeployed for the update master page to appear.
Finally, the VirtualPathProvider in ASP.NET 2.0 can serve files that
do not exist on the file system. With the VirtualPathProvider, a set
of master pages could live in database tables that all applications
use. For an excellent article on the VirutalPathProvider, see “Virtualizing Access to Content: Serving Your Web Site from a ZIP File”.
Keep a primary copy in source control, and let your source control system worry about it.
Supposing you can create a common repository for all your projects (a common folder in your source control tree, for example), you could add the master pages as links by using relative paths.
However, IIRC, Visual Studio makes local copies of files added from external paths. You might have to text-edit the solution/project file to add the linked files.
This, of course, is assuming you use “Web Application” format. Older VS “Web Sites” do not have project files and rely on having all files within the site folder.
AFAIK there is no elegant way to do what you are looking to.. VS will always end up copying it.
I think to be honest it may not be a great idea.. Obviously you want to share the lowest common code, but a whole MasterPage?.. Sounds like you could be asking for trouble since one minor change could have such an impact on one or more applications..
I would suggest instead seperating out the good bits of functionality into components/controls and deploying them.
I cant see why you would want to have the same markup accross different projects, if you do, I dont know an easy way.
However with code, obviously you can write a code file which inherits from
Put in whatever logic you want in all your master pages in there. Build it as your awesome dll then just include that in your projects.
Use a symbolic link:
A symbolic link is a file-system object that points to another file
system object. The object being pointed to is called the target.
Symbolic links are transparent to users; the links appear as normal
files or directories, and can be acted upon by the user or application
in exactly the same manner.