asp.net - 发布项目时如何交换连接字符串?

标签 asp.net visual-studio-2010 iis web-config publishing

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

-web.Debug.config
-web.Release.config

在此配置文件中,有以下注释:

In the example below, the "SetAttributes" transform will change the value of "connectionString" to use "ReleaseSQLServer" only when the "Match" locator finds an atrribute "name" that has a value of "MyDB".


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

什么是"Match" locator ?
我已经在 Web.config 中有一个连接字符串那么我该如何设置呢?
主 web.config 文件应该包含生产连接字符串还是其他方式?
我正在寻找做过类似事情的人的建议

最佳答案

我们正在使用 xdt:Transform="Replace",它基本上替换了我们开发数据库的连接字符串,并且运行良好。见下文:

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

<connectionStrings>
  <add name="MyDB" connectionString="Data Source=DebugSQLServer;Initial Catalog=MyDebugDB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

生产连接字符串(在您的情况下为 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>

关于asp.net - 发布项目时如何交换连接字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19324470/

相关文章:

当另一个 HTML 页面向其提交表单时,HTML 页面抛出 405 错误

c# - IIS/asp.net 中的静态方法和调用堆栈

c# - Asp.net session 时间太短

c# - 使用 ASP.NET Web API,我的 ExecutionContext 不在异步操作中流动

visual-studio-2010 - 如何将 const wchar_t* 转换为 wchar_t 或多字节 char?

qt - 无法使用 MSVC 2010 x86 构建 qt5.2.1 静态

c# - Convert.FromBase64String(...) 抛出 FormatException

asp.net - 对 HTML/ASP.NET 源代码进行拼写检查的软件

asp.net - ASPxPopupControl 总是在另一个 ASPxPopupControl 前面

asp.net - 如何在 mvc 2 中将复杂数据作为模型从 View 传递到 Controller