Question

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.

Was it helpful?

Solution

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.

The third bullets near the end the article tells you of possible ways you can share Masterpages also.

OTHER TIPS

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

System.Web.UI.MasterPage

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top