【问题标题】:How do I use Web.Config transform on my connection strings?如何在我的连接字符串上使用 Web.Config 转换?
【发布时间】:2012-01-12 01:24:32
【问题描述】:

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

<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

【问题讨论】:

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


    【解决方案1】:

    这对我有用,但我也发现它有时有点古怪。 您将需要创建另一个名为 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>
    

    【讨论】:

    • appSettings 部分是可选的,具体取决于您设置连接的方式。
    • appSettings 部分到底是做什么的?另外,假设我可以使用aspnet_regiis -pef 加密转换后的connectionStrings 是否安全?
    • 我使用 appSettings 以防我有多个连接字符串 Local Db 与托管在另一台机器上的连接字符串。这纯粹是为了方便,所以我可以使用 appsetting 来决定使用哪个数据库。 (我真的应该把它排除在这个例子之外)
    【解决方案2】:

    您不需要创建新文件,它应该在解决方案资源管理器中,展开 Web.config,然后打开 Web.Release.config。

    Scott Allan 有一个很好的视频 here(在配置和部署 > 配置转换下)。

    【讨论】:

    • 有机会重新链接到视频吗?自 2011 年以来,pluralsight 似乎已经改变了他们的目录结构:-(
    • 这个视频正是我所需要的。谢谢。
    • 链接是付费墙。
    • 该死的,那个链接以前是免费视频的。对不起
    猜你喜欢
    • 1970-01-01
    • 2013-05-20
    • 2012-09-25
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多