如何在发布项目时交换连接字符串?

med*_*eda 4 asp.net iis web-config publishing visual-studio-2010

我可以看到该Web.config文件包含两个文件:

-web.Debug.config
-web.Release.config
Run Code Online (Sandbox Code Playgroud)

在这个配置文件中有以下注释:

在下面的示例中,仅当"匹配"定位器找到具有值"MyDB"的属性"名称"时,"SetAttributes"转换才会将"connectionString"的值更改为使用"ReleaseSQLServer".

<connectionStrings>
  <add name="MyDB" 
    connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" 
    xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)

什么是"Match" locator?我已经有一个连接字符串了,Web.config那我该怎么设置呢?主web.config文件应该包含生产连接字符串还是反过来?我正在寻找那些做过类似事情的人的挚爱

gre*_*jer 7

我们正在使用xdt:Transform ="Replace",它基本上替换了我们的开发DB的连接字符串,它完美地工作.见下文:

开发连接字符串(在您的情况下是web.Debug.config):

<connectionStrings>
  <add name="MyDB" connectionString="Data Source=DebugSQLServer;Initial Catalog=MyDebugDB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)

生产连接字符串(在您的情况下是web.Release.config):

<connectionStrings xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <add name="MyDB" connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" xdt:Transform="Replace" xdt:Locator="Match(name)" />
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)