在项目之间共享母版页的最佳方法是什么

时间:2020-03-05 18:44:01  来源:igfitidea点击:

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

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

解决方案

回答

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

回答

使用符号链接:

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.

回答

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

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

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

回答

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

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

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

回答

摘自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”.

回答

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

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

System.Web.UI.MasterPage

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

回答

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

我们可以查看以下几个资源。

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

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