我知道这我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