关于.net:跨项目共享母版页的最佳方法是什么

关于.net:跨项目共享母版页的最佳方法是什么

What is the best way to share MasterPages across projects

假设您有两个单独的ASP.NET Web应用程序项目,都需要使用一个公共的MasterPage。

在无需重复代码的情况下跨项目共享MasterPage的最佳方法是什么? 最好不必诉诸于源代码控制或文件系统黑客。


我试图完成同样的事情。我研究了几种解决方案,但我认为使用虚拟目录可能是共享母版页的最佳方法。

您可以查看以下几个资源。

  • 在其中共享母版页
    通过将其嵌入到应用程序中
    Dll
  • 在Visual Studio中共享母版页
  • ASP.Net 2.0-母版页:技巧,窍门和陷阱

文章结尾处的第三个项目符号告诉您共享母版页的可能方法。


摘自K.Scott Allen的ASP.Net母版页:"共享母版页"上的技巧,窍门和陷阱文章:

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".


将主副本保留在源代码管理中,并让您的源代码管理系统担心它。


我不明白为什么您要在不同项目中使用相同的标记,如果您这样做,我不知道一种简单的方法。

但是,使用代码,显然您可以编写一个代码文件,该文件继承自

1
System.Web.UI.MasterPage

在其中的所有母版页中放入所需的任何逻辑。将其构建为您出色的dll,然后将其包含在您的项目中。


AFAIK没有优雅的方法可以完成您想要的工作。VS总是会最终将其复制。

我想说实话,这可能不是一个好主意。显然,您希望共享最低的通用代码,但是要共享整个MasterPage ?。听起来像是您可能会自找麻烦,因为一个小的更改可能会对一个这样的影响或更多的应用程序..

我建议改为将功能的优点分离到组件/控件中并进行部署。


假设您可以为所有项目创建一个公共存储库(例如,源代码管理树中的公共文件夹),则可以使用相对路径将母版页添加为链接。

但是,在IIRC中,Visual Studio会创建从外部路径添加的文件的本地副本。您可能需要对解决方案/项目文件进行文本编辑以添加链接的文件。

当然,这是假设您使用" Web应用程序"格式。较早的VS"网站"没有项目文件,而是依赖于将所有文件包含在站点文件夹中。


使用符号链接:

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.


推荐阅读