【问题标题】:Moving a custom configuration group to a separate file将自定义配置组移动到单独的文件
【发布时间】:2010-12-06 11:14:31
【问题描述】:

我最近编写了一个相当大的自定义配置组。我很好奇是否可以通过以下方式将此配置移动到单独的文件中:

<configuration>
    <configSections>
        <sectionGroup name="MyCustomGroup">
            <section name="MyCustomSection"/>
        </sectionGroup>
    </configSections>
    <MyCustomGroup file="alt.config" />
</configuration>

这类似于您可以对 appSettings 的文件属性执行的操作。我意识到很可能需要为我的自定义部分处理程序创建一个 ConfigurationPropertyAttribute,但是我没有成功找到这方面的任何示例或方向。

【问题讨论】:

    标签: .net handler custom-configuration


    【解决方案1】:

    据我所知,您不能使用configSource 属性将整个SectionGroup(即MyCustomGroup)外部化,但您必须在Section 级别处理此问题(即MyCustomSection )

    <configuration>
        <configSections>
            <sectionGroup name="MyCustomGroup">
                    <section name="MyCustomSection"/>
            </sectionGroup>
        </configSections>
        <MyCustomGroup>    
           <MyCustomSection configSource="externalfile.config" />
        </MyCustomGroup>
    </configuration>
    

    外部文件externalfile.config 将包含您的实际配置设置,直接从您自己的自定义部分标签开始(没有前导&lt;?xml....?&gt;&lt;configuration&gt; 或任何需要的东西):

    <MyCustomSection>
        ... your settings here......
    </MyCustomSection>
    

    马克

    【讨论】:

    • 你是对的。部分组不能作为一个整体外化,但其部分可以。
    • @marc_s - 太好了,我没有意识到这是一个如此古老的问题。我只是在谷歌上搜索,发现这是最好的结果!
    • 正是我所需要的。
    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 2018-06-28
    • 2022-07-19
    • 1970-01-01
    • 2021-01-26
    • 2012-01-07
    • 1970-01-01
    • 2020-11-25
    相关资源
    最近更新 更多