Sharing Master Pages in Visual Studio
After you have created a set of master pages, you might want to make them available to others, such as other developers creating Web applications in your company. In most cases, you probably do not want other developers to change the master pages. There are several ways you can make your master pages available for reuse.
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.
For more information on creating a virtual path provider or using virtual paths to provide content, see article 910441, "How to use virtual path providers to dynamically load and compile content from virtual paths in ASP.NET 2.0" in the Microsoft Knowledge Base and Virtualizing Access to Content: Serving Your Web Site from a ZIP File.
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.
See Also
Tasks
Walkthrough: Creating and Using ASP.NET Master Pages in Visual Web Developer