如何创建一个通用的Web.config文件和一堆特定的Web.config文件?
目前
,我正在开发一个 ASP.NET 项目,该项目在版本控制下托管,并在多个开发人员计算机、测试人员计算机和生产环境上使用。
在三种情况下,配置 (Web.config) 可能不同。例如,开发人员和测试人员环境使用测试 SQL Server,而在生产环境中访问另一个 SQL Server,因此在这些情况下连接字符串是不同的。
我们希望在 subversion 中保留三个版本的 Web.config。但是,每次我们需要添加、删除或更改公共设置时,都要修改三个文件中的每一个,这很烦人:如果有一个公共的主 Web.config 会很好,三个 Web 都会继承它。配置文件。
问题
如何设置一个 ASP.NET 项目,在不同的机器上使用主配置文件和不同的从配置文件,从而在 subversion 中共享相同的项目/源代码/配置文件?
如果 .NET Framework 无法做到这一点,有哪些替代方案(以避免重新发明轮子和/或在需要构建自定义解决方案时查看它是如何工作的)?
Context
Currently, I work on an ASP.NET project which is hosted under version control and is used on several developer machines, tester machine and production environment.
In three cases, configuration (Web.config) may be different. For example, developer and tester environments use testing SQL Server, whereas in production environment, another SQL Server is accessed, so the connection string is different in those cases.
We want to keep three versions of Web.config in subversion. But modifying each of three files every time we need to add, remove or change a common setting is annoying: it would be nice to have a common, master Web.config, which will be inherited by each of the three Web.config files.
Question
How to set up an ASP.NET project which will use a master configuration file and different slave configuration files on different machines, thus sharing the same project/source code/configuration files in subversion?
If .NET Framework is unable to do that, what are the alternatives (to avoid reinventing the wheel and/or to see how it works if there is a need to build a custom solution)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Visual Studio 2010 具有此功能,称为“web.config 转换”。您有一个基本的 web.config,然后,对于每个配置,您都有转换文件,可以有效地“编辑”特定环境的基本 web.config。
默认 Web.Debug.config 文件的示例:
Visual Studio 2010 has this feature, called "web.config transforms". You have a base web.config, then, for each configuration, you have transform files, that effectively "edit" the base web.config for the particular environment.
Example from the default Web.Debug.config file:
如果我从今天开始,VS 2010 的 web.config 转换功能绝对值得一看。但在工具支持可用之前我必须解决这个问题,而且我也认为转换很好,但有时它们只是不能满足要求——特别是如果你有很多严重配置驱动的东西或可以改变的生产配置在开发团队不知情的情况下。
我们解决这个问题的方法是保留配置的单独副本。存储在项目根目录中的是我们的标准化开发环境。它还被设置为“内容”文件,不会复制到输出文件夹。从 Visual Studio 的角度来看,环境文件存储在项目外部,但仍然位于相同的版本控制中,位于每个环境和方面的文件夹中(我们有许多带有单独管理工具的多头 Web 应用程序)。我们使用构建脚本将它们结合在一起,该脚本根据声明的环境复制正确的配置。
我们发现这比实践中的转换要好得多——主要是生产配置如此不同,以至于转换可能比配置更多。还允许我们保持生产配置同步,因为它实际上只是文件副本,而不是某些可怕的 XSLT 的更新。
If I were starting today, the web.config transforms feature of VS 2010 is definitely worth a look. But I had to solve this problem before tool support was avaliable, and I also think that transforms are nice, but sometimes they just don't cut the mustard -- especially if you have lots of heavily configuration driven stuff or production configurations that can change without the dev team knowing about it.
How we tackle this is to keep separate copies of the configurations. The one stored within the project root is for our standardized development environment. It is also set as a "content" file which is not copied to the output folder. The environmental ones are stored outside the project from a visual studio standpoint but still in the same version control, in a folder for every environment and facet (we have lots of multi-headed web apps with separate admin tools). We tie it all together using a build script that copies the right configurations depending on the declared environment.
We have found this works quite a bit better than the transforms in practice -- main thing being the production configurations are so different that there is perhaps more transform than configuration. Also allows us to keep the production configuration in synch as it is really just a file copy, not an update of some horrid XSLT.