c# - 如何在我的连接字符串上使用 Web.Config 转换?

标签 c# asp.net-mvc-3 web-config connection-string

在我当前的项目中,我有一些对本地开发机器有效的连接字符串:

<configuration>
  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="Data Source=localhost;Initial Catalog=MyDB;Integrated Security=SSPI"
  </connectionStrings>
....
</configuration>

我如何使用 Web.Config 转换将此表达式转换为对我们的生产服务器有效的表达式?生产服务器看起来像这样:

<configuration>
  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="Data Source=IPAddress,Port;Initial Catalog=SomeOtherDB;User ID=TopSecretUsername;Password=SecurePassword"
  </connectionStrings>
....
</configuration>

语法对我来说并不明显,我完全无法理解 page在上面。

最佳答案

这对我有用,但我也发现它有时有点古怪。 您将需要创建另一个名为 Web.Config.Release 的文件并用以下内容填充它:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

  <connectionStrings>
    <add name="local" connectionString="Data Source=IPAddress,Port;Initial Catalog=SomeOtherDB;User ID=TopSecretUsername;Password=SecurePassword" 
    xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  </connectionStrings>

  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />

  </system.web>
    <appSettings>
        <add key="default_db_connection" value="local" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
    </appSettings>
</configuration>

关于c# - 如何在我的连接字符串上使用 Web.Config 转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8366091/

相关文章:

c# - 在 C# 中继承泛型的一个很好的理由

asp.net-mvc - 为什么我的 View 模型中的内部成员无法在 View 中访问?

asp.net-mvc-3 - MVC3、Razor View 、EditorFor、查询字符串值覆盖模型值

ASP.NET MVC 3 : best way manage a model with an image

asp.net - App_Code 和 web.config

c# - 我应该将自定义类放在 ASP.NET Core 项目中的什么位置

c# - 在 azure 辅助角色中使用 QueueClient.OnMessage

c# - ERPTable 列被截断

security - 加密 Web.Config 中的连接字符串 - 为什么?

asp.net - Web.Config 奇怪的 <customErrors> 部分?