如何为.Net包含一个配置文件

15

有没有一种方法可以让主.NET配置文件app.config / web.config包含另一个配置文件?我需要将事物保持在单独的文件中,但是将它们链接在一起。

这是我想要包含的示例:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="LocationSearch.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <applicationSettings>
    <LocationSearch.Properties.Settings>
        <setting name="MapQuestApiKey" serializeAs="String">
        <value>some value here...</value>
        </setting>
        <setting name="MapQuestClientCode" serializeAs="String">
        <value>another value here...</value>
        </setting>
    </LocationSearch.Properties.Settings>
  </applicationSettings>
</configuration>

我在主配置文件中添加了一个XML示例。 - Paul Fryer
3个回答

13

可以,使用 'file' 属性即可

<appSettings file="configFiles/otherConfigFile.config">

MSDN信息

关于使用'file'属性的MSDN文章中的一些相关信息:

独立文件的内容与Web.config文件中的appSettings部分合并。这个功能仅限于appSettings属性。

注意:在.NET Framework 2.0版本中,您现在可以为所有支持configSource属性的配置元素包含配置设置,但是当您使用configSource属性时,必须将整个部分移动到单独的文件中,因为没有元素设置的合并。


9

是的,你可以使用configSource属性将单独的文件包含到主.config文件中,如下所示:

<securityConfiguration configSource="SomeOtherConfigFile.config" />

通过这种方式包含的外部配置文件必须与主.config文件在同一目录(或子文件夹)中。

SectionInformation.ConfigSource 属性


不错!为什么要将该元素称为“securityConfiguration”?我猜想应该是期望类似于“<include configSource...”这样的名称。 - Paul Fryer
我相信你可以在任何配置部分中使用configSource属性。我只是以securityConfiguration为例,因为它在我手头的配置文件中 :) - Kevin Tighe
听起来你只需要在任何元素中添加“configSource”属性,它就会注入链接文件中的内容,对吗? - Paul Fryer
3
我相信你只能在 ConfigurationSection 中使用 configSource 属性,例如 appSettings、securityConfiguration 等。 - Kevin Tighe
请注意,使用“configSource”的部分可能不包含其他属性或元素。 如果您希望在主配置文件中拥有一些appSettings键,而其他键在包含文件中,可以使用<appSettings file="include.config">(正如Jim Ecker在这个问题的其他答案中提议的那样)。 - undefined

1

是的,我为URL重写规则做这个。

所以在我的web.config中,我这样做:

<urlrewritingnet configSource="UrlRewriting.config" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接