Asp.net

Web.Debug.config 不會將連接字元串轉換為 MVC 5 項目中的 Web.config

  • February 3, 2020

我創建了一個新的 VS 2015 Web 項目 MVC5。預設情況下,我可以看到Web.configWeb.Debug.config

閱讀了幾篇文章,我真的看不出我真正需要做什麼才能讓它從 Web.Debug.config 中獲取我的值並替換目前的 Web.config。

我一直在看另一個工作中的項目,它可以做到這一點並且工作正常,但我經歷了很多屬性和設置,我看不出有什麼不同。

我可以右鍵點擊 Web.Debug.config 和預覽,它告訴我它將用“10.10.10.10”替換“測試”,所以對我來說似乎很好(就像它應該工作但執行項目它不會改變值)

例子

項目 :

調試/任何 CPU,執行Google瀏覽器,問題是數據源沒有改變

Web.Debug.config

<connectionStrings>
   <add name="Envy" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
           xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
   <add name="EnvyIdentity" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
       xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
   <add name="DNNSmartstore" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
       xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
   <add name="DNNPos" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=DevFood_POS;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
       xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
 </connectionStrings>

網頁配置

 <connectionStrings>
   <add name="Envy" connectionString="Data Source=test\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
   <add name="EnvyIdentity" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
   <add name="DNNSmartstore" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
   <add name="DNNPos" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=DevFood_POS;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
 </connectionStrings>

開箱即用,轉換(調試/發布)應用於發布(部署)。不在建構時,在部署時。

要在建構時實現這一點,您可能需要對項目文件進行一些手動編輯。看看這裡的例子:https ://gist.github.com/EdCharbeneau/9135216

如此接近但向後。對於這種情況,僅使用Web.configWeb.Release.config

將所有 Debug 設置放在Web.config中,將 Release 設置放在Web.Release.config中,不要使用Web.Debug.config。將使用正常 Web.config 進行調試,並且只有發布的版本才能獲得發佈設置。

從你上面的例子:

網頁配置

<!-- test/debug settings in regular web.config -->
   <connectionStrings>
       <add name="Envy" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
       <add name="EnvyIdentity" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
       <add name="DNNSmartstore" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
       <add name="DNNPos" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=DevFood_POS;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>       
     </connectionStrings>

Web.Release.config

<connectionStrings>
   <add name="Envy" connectionString="Data Source=test\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
       xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
   <add name="EnvyIdentity" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
       xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
   <add name="DNNSmartstore" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
       xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
   <add name="DNNPos" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=DevFood_POS;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
       xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
 </connectionStrings>

Web.Debug.config是放置部署到測試伺服器時要應用的設置的地方。這個名字起初很容易誤導。我使用此功能的唯一地方是如果我有一個需要與我的開發框不同的設置的測試伺服器。:-)

引用自:https://stackoverflow.com/questions/36851072