当我开发我的web应用程序时,我需要在3个不同的环境之间切换-开发,UAT和Prod。我在我的配置文件中对所有3个都有不同的数据库连接。我看到过通过更改所有引用然后重新构建解决方案以及使用预处理器指令来手动切换这些设置。有没有一种简单的方法可以基于某个变量来实现这一点,以便在每次部署到新环境时不必修改配置?
发布于 2008-09-25 12:08:28
在我看来,你可以从Visual Studio 2005 Web Deployment Project中获益。
这样,您就可以告诉它根据构建配置更新/修改web.config文件的各个部分。
请看一下this blog entry from Scott Gu,以获得快速概述/示例。
发布于 2008-09-25 12:14:22
我非常喜欢使用MSBuild,特别是XSLT社区任务( MSBuild Community task,http://msbuildtasks.tigris.org/),其中有一个任务可以使用适当的连接字符串设置来转换web.config,等等。
我把这些任务放在手边:
<Target Name="Configs">
<Xslt RootTag="" Inputs="web.config" Output="Web.$(COMPUTERNAME).config" Xsl="web.config.$(COMPUTERNAME).xslt" Condition="Exists('web.config.$(COMPUTERNAME).xslt')" />显然,这不是你想要的100%,它是为了让每个开发人员都有自己的web.config。
但是,您没有理由不能使用上述原则来拥有应用正确XSLT的多个构建配置。
我的XSLT如下所示:
<?xml version="1.0" encoding="utf-8"?><!-- Dev -->
<xsl:template match="/configuration/connectionStrings/add[@name='MyConnectionString']/@connectionString">
<xsl:attribute name="connectionString">Data Source=MyServer;Initial Catalog=MyBD;User ID=user;password=pwd</xsl:attribute>
</xsl:template>
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
发布于 2008-09-25 12:06:12
Scott Hanselman提出了一种方法来做到这一点:
http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx
https://stackoverflow.com/questions/132885
复制相似问题