多个 WebConfig 合二为一
我们的网站有一个生产服务器和一个开发服务器。我每个都有不同的设置和许可证信息,并且保存在 2 个不同的 web.config 文件中。
有没有一种方法可以将它们合并到一个 web.config 文件中?这样我在发布网站时就不会用开发版本覆盖生产版本(反之亦然)。
We have a production server and a development server for our site. I have different settings and license information for each and that is held in 2 different web.config files.
Is there a way to consolidate those to one web.config file? So that I don't overwrite production's with development's (or vice versa) when I publish the site.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 VS 2010,您可以通过 web.config 转换和构建配置来完成此操作。 Scott Guthrie 对此写道:
http:// /weblogs.asp.net/scottgu/archive/2010/07/29/vs-2010-web-deployment.aspx
我认为这基本上只需创建额外的配置文件(例如 Web.Debug. config),VS 会自动将它们“链接”到 IDE 中的主 web.config,但我无法在我现在使用的计算机上验证它,因为它没有 VS 2010。
您也可以相当轻松地做到这一点在旧版本的 VS 中可以使用构建事件,但它并不那么好。斯科特实际上也写过这一点:
http://weblogs.asp.net/scottgu/archive/2007/09/21/tip-trick-automating-dev-qa-staging- and-product-web-config-settings-with-vs-2005.aspx
(摘自 Scott 的博客文章)这里是您执行此操作所采取的高级步骤。它们可以与 VS 2005 和 VS 2008 一起使用。
With VS 2010, you can do this with web.config transformations and build configurations. Scott Guthrie wrote about this:
http://weblogs.asp.net/scottgu/archive/2010/07/29/vs-2010-web-deployment.aspx
I think this basically works by you just creating your additional config files (such as Web.Debug.config) and VS automatically "links" them to your main web.config in the IDE, but I can't verify it at the computer I am using right now since it doesn't have VS 2010.
You can also do it fairly easily with build events in older versions of VS, but it isn't as nice. Scott actually wrote about this too:
http://weblogs.asp.net/scottgu/archive/2007/09/21/tip-trick-automating-dev-qa-staging-and-production-web-config-settings-with-vs-2005.aspx
(Taken from Scott's blog post) Here the high-level steps you take to do this. They work with both VS 2005 and VS 2008.
您可以创建 Web 部署项目 (VS2008、VS2010) 这是一个 XML MSBuild 文件,允许您在构建特定配置文件时将 XML 节点替换/插入/替换到配置文件中。您可能会花费半天到一天的时间来正确配置它,但这些时间是非常值得的,因为它可以为您省去很多部署麻烦(也让您更接近一键部署)。
注意:对于 VS2008 Web 部署项目,您可能需要使用 MSBuild 社区任务。我不知道 VS2010 的这些任务的状态如何 - 是否仍然需要它们。
对于使用 MSBuild 社区任务(我们在 VS2010 下使用)的 VS2008 Web 部署任务,它看起来有点像......
在我们的 Web 应用程序目录中,我们有一个文件
web.Substitutions.config
,它看起来是有点像...值得一提的是,我们使用 Team City 来运行我们的 Web 部署项目,无需额外配置必要的并且它可以顺利工作。 Hudson 也是同样的方式(并且设置起来更容易一些)。
You can create a web deployment project (VS2008, VS2010) which is an XML MSBuild file that lets you replace/insert/substitute XML nodes into the configuration file when you build for a particular profile. You'll probably spend a half a day to a day getting it configured correctly but the time is well worth it as it can save you a lot of deployment headaches (also puts you one step closer to one-click deployments).
Note: for the VS2008 web deployment projects you will probably want to use the MSBuild Community Tasks. I don't know what the status is for VS2010 with these tasks - whether or not they are still required.
For our VS2008 web deployment tasks using the MSBuild Community Tasks (which we use under VS2010) it looks a little like...
In our web application's directory we have a file
web.Substitutions.config
which looks a bit like...Worth mentioning that we use Team City to run our web deployment projects with no additional configuration necessary and it works without a hitch. Hudson is the same way (and a bit easier to setup).
这只是一种可能性,但您可以合并它们,根据上下文为值提供适当的名称,并使用代码中的编译常量有条件地访问每个元素。例如,在您的 web.config 中,您可能有...
然后,在您可能有的 Configuration 类中...
现在,考虑到定义了 DEBUG 常量,那么,在每个环境构建时,使用此属性将通过适当的设置。
Just one possibility, but you could merge them giving appropriate names to values based on the context and access each element conditionally using compilation constants in code. For instance, in your web.config you may have...
Then, within, let's say, a Configuration class you may have...
Now, considering the DEBUG constant is defined, then, when building per environment, using this property would pull through the appropriate setting.