我们在.net开发/发布过程中,需要根据环境的不同去修改一些琐碎的web.config配置,比如:调试的时候数据库连接的是localhost或者dev环境的DB,发布到Test环境时,需要将数据库连接改为TestDB,当项目上线时,我们又需要修改数据库连接为生产环境下的DB…
具体做法如下:
1.web.config文件内容如下
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="testKey" value="any value"/>
<add key="key1" value="abc"/>
</appSettings>
<connectionStrings>
<add name="connStr1" connectionString="template content"/>
<add name="connStr2" connectionString="template content"/>
<add name="connStr3" connectionString="template content"/>
<add name="connStr4" connectionString="template content"/>
<add name="connStr5" connectionString="template content"/>
<add name="key1" connectionString="abcd"/>
</connectionStrings>
2.Web.Release.config文件内容如下
<appSettings >
<add key="testKey" value="test" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
</appSettings>
<connectionStrings xdt:Transform="Replace">
<add name="connStr1" connectionString="release1"/>
<add name="connStr2" connectionString="release2"/>
<add name="connStr3" connectionString="release3"/>
<add name="connStr4" connectionString="release4"/>
<add name="connStr5" connectionString="release5" />
<add name="some_string_else" connectionString="release else"/>
<add name="key1" connectionString="release1213"/>
</connectionStrings>
3.Publish后的web.config文件为:
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="testKey" value="TestConfig test value" />
<add key="key1" value="abc" />
</appSettings>
<connectionStrings>
<add name="connStr1" connectionString="release1" />
<add name="connStr2" connectionString="release2" />
<add name="connStr3" connectionString="release3" />
<add name="connStr4" connectionString="release4" />
<add name="connStr5" connectionString="release5" />
<add name="some_string_else" connectionString="release else" />
<add name="key1" connectionString="release1213" />
</connectionStrings>
如果报错"未定义命名空间前缀xdt" ,则需要声明
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"></configuration>
4.修改项目的配置文件
比如XXX.csproj或XXX.vbproj(csproj或vbproj取决于你使用的语言是C#还是VB.NET),把最后的注释掉的BeforeBuild和AfterBuild部分取消注释,修改为:
<Target Name="BeforeBuild">
<TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
</Target>
<Target Name="AfterBuild">
</Target>
5.connectionStrings 引用其他路径文件
如果web.config里面<connectionStrings configSource="Configs\database.config" />
,则需在XXX.csproj里面进行如下配置database.config:
<Target Name="BeforeBuild">
<TransformXml Source="Configs\database.config" Transform="Configs\database.$(Configuration).config" Destination="Configs\database.config" />
</Target>
<Target Name="AfterBuild">
</Target>
6.注意事项
①connectionStrings 配置都在web.config文件里面
web.config文件应以开头,且不包含任何xmlns:xdt内容
web.$(Configuration).config文件包含任何xmlns:xdt内容
②connectionStrings 配置在database.config文件里面,web.config里面<connectionStrings configSource="Configs\database.config" />
<connectionStrings configSource="Configs\database.config" />
则database.config文件需以connctionStrings节点包含元素,
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
</connectionStrings>
否则会报错:configSource 文件的格式必须为包含节名的元素。
同①中的注意事项,database.config不包含任何xmlns:xdt内容。
database.Debug.config配置如下:
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xdt:Transform="Replace">
</connectionStrings>
database.Release.config同database.Debug.config配置一样。