2011-12-03 83 views
22

在我當前的項目,我有一些連接字符串,對地方發展的機器是有效的:如何在我的連接字符串上使用Web.Config轉換?

<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> 

的語法並不明顯給我,我完全在所著的Grokking就可以了page失敗。

回答

37

這適用於我,但我也發現它有時有點flakey。 您將需要創建一個名爲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> 
+0

appSettings部分是可選的,具體取決於您如何設置連接。 – sbeskur

+0

'appSettings'部件究竟做了什麼?另外,假設我可以使用'aspnet_regiis -pef'加密轉換後的'connectionStrings'是否安全? –

+0

我使用appSettings的情況下,我有多個連接字符串本地Db VS一個託管在不同的機器上。這是純粹的便捷驅動,所以我可以使用appsetting來決定使用哪個數據庫。 (我真的應該只是把它從這個例子中刪除) – sbeskur

7

你不應該需要創建一個新的文件,它應該是在Solution Explorer中,展開Web。配置,並打開Web.Release.config。

Scott Allan有一個很好的視頻here(在配置和部署>配置轉換下)。

+0

你有可能重新鏈接到視頻嗎?看起來像自2011年以來,多元視覺已改變他們的目錄結構:-( – 5arx

+0

視頻鏈接已更新。 – JasonH

+1

此視頻正是我需要的。謝謝。 – MartinJH

相關問題