我知道這我s太舊了,可能甚至不適用於.NET Core,但對於那些來自Google並使用非.NET Core json配置文件的應用程序。以下是我通常所做的...
我使用configSources
從web.config
中取出所有配置設置。這可以讓你到一個特定的配置節到不同的文件例如通過提供相對位置這裏是如何你會在聲明配置節一configSource
(根web.config
文件)...
<configuration>
<log4net configSource="Config\debug\log4net.config" />
<appSettings configSource="config\debug\settings.config" />
<connectionStrings configSource="config\debug\connections.config" />
...
</configuration>
你可以只要確定指定的路徑和文件存在於解決方案中,就可以命名這些文件。下面介紹一下的settings.config文件看起來像......
<?xml version="1.0"?>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="foo" value="bar" />
</appSettings>
現在,相對路徑是相對於項目的根......
在上圖中可以看到,我爲不同的部署環境提供了兩條不同的路徑,這是因爲顯然我的連接字符串和設置在生產中是不同的。
然後你可以使用配置的轉換,使應用程序能夠使用正確的配置文件,無論是在調試或發行模式...
這就是Web.Debug.config
文件的樣子..
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<log4net configSource="Config\debug\log4net.config" xdt:Transform="Replace" />
<appSettings configSource="config\debug\settings.config" xdt:Transform="Replace" />
<connectionStrings configSource="config\debug\connections.config" xdt:Transform="Replace" />
</configuration>
發行版本幾乎是一樣的...替換提供給configSource屬性的路徑。這就是它。 還有其他web.config元素支持configSource設置,例如很多system.serviceModel
子元素。
來源
2017-08-16 01:51:52
Leo
看看這個:http://stackoverflow.com/questions/11363121/connectionstring-management-for-many-projects-on-one-server-should-i-make-my-ow/11363291#11363291 – 2013-05-14 16:35:42