The simplest solution is to copy the master pages to a file directory that other projects can then reference. Developers could navigate to the directory that contains the master pages and then add them to a Web application project. If you want to control who can modify the master pages, you can set security permissions on the directory that contains the master pages. The additional solutions outlined below allow greater flexibility in where you store the master pages intended for reuse.
Use Virtual Directories
Another solution would be to store the master pages in a virtual directory. Virtual directories are a convenient way to set up a site for local Web development work because they do not require a unique site identity. This means that a virtual directory requires fewer steps than it would to create a unique site to store your master pages. Virtual directories are maps to the physical location of files that a Web application can use. The files do not have to be under the root of the Web application. This approach enables you to maintain one set of master pages in one location that various Web applications residing on remote machines can use.
To use master pages in a virtual directory, a developer would navigate to the directory and add the master pages to the current Web application project. For more information about virtual directories, see How to: Create IIS Virtual Directories in Visual Studio or How to: Create and Configure Virtual Directories in IIS 5.0 and 6.0.
Use Virtual Paths
You can use virtual paths to provide content from locations other than the file system. If, for example, you store your master pages in a Microsoft SQL Server database, you can create a virtual path provider to dynamically retrieve content from the database. You can also use virtual paths to provide content from a compressed .zip file.
To create a virtual path provider that can be used to provide content and files for compilation in another ASP.NET version 2.0 Web application, the developer must:
- Create a VirtualPathProvider class and implement all the required methods to handle the master page file requests.
- Register the virtual path provider to let the ASP.NET hosting environment know where the master pages are located.
- Create VirtualFile and VirtualDirectory objects to stream the content.
Precompile the Code Used in a Master Page
If you are concerned about code in your master pages being visible to others reusing the pages, you can precompile the master pages' code into a library. In this library, you can include code-behind pages as well as user or custom controls. Compiling master pages does not remove the declarative code for the master files or any server controls used, but you can compile the master files to remove the code for controls or code-behind pages used by the master pages.
If you choose to compile the master pages into a library, you must use the "updatable" build option that allows for later modification of the markup. This option is determined by the Allow the precompiled site to be updatable check box in the Publish Web Site dialog box. For more information about precompiling pages into a library that can be reused, see Building Re-Usable ASP.NET User Control and Page Libraries with VS 2005.