【问题标题】:Accessing an attribute on a configSection in a web.config?访问 web.config 中 configSection 的属性?
【发布时间】:2009-12-28 08:54:28
【问题描述】:

解决方案: 只是添加解决方案:sectionGroups 似乎没有属性。正确的方法似乎是将ConfigurationSection 作为父母,将ConfigurationElement 作为每个孩子。还有 ConfigurationElementCollection 用于收藏。 .net Framework 中的一个示例:<roleManager> 是一个 Section,<providers> 是一个 ElementCollection。我blogged about my solution

原始问题:我的 web.config 中有一个自定义 sectionGroup:

<sectionGroup name="myApp" type="MyApp.MyAppSectionGroup">
  <section name="localeSettings" 
           type="MyApp.MyAppLocaleSettingsSection"/>
</sectionGroup>

sectionGroup 本身应该有一个属性:

<myApp defaultModule="MyApp.MyAppTestNinjectModule">
  <localeSettings longDateFormat="MM/dd/yyyy HH:mm:ss" />
</myApp>

我无法访问该属性 (defaultModule)。使用ConfigurationManager.GetSection("myApp/localeSettings") 获取一个部分非常容易,并将其转换为从 ConfigurationSection 继承的类。

但我似乎无法轻松访问 sectionGroup,因为 ConfigurationManager.GetSection("myApp") 返回 null。我试过ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).SectionGroups["myApp"],但这并没有实现一个索引器,让我可以访问defaultModule

我是不是误会了什么? sectionGroups 真的只是没有自己设置的容器吗?我可以在没有 sectionGroup 的情况下嵌套部分吗? OpenExeConfiguration 适合 .net 应用程序(使用 web.config)还是只适合 app.config 的?

编辑:感谢有关 WebConfigurationManager 而不是 ConfigurationManager 的提示。这并没有解决我的主要问题,但至少这有更明智的命名 OpenXXX 方法。

目前,这是两个类。 LocaleSettings 工作得很好,只是 SectionHandler 不允许我访问“defaultModule”属性。

public class MyAppSectionGroup: ConfigurationSectionGroup
{
    [ConfigurationProperty("localeSettings")]
    public MyAppLocaleSettingsSection LocaleSettings
    {
        get
        {
            return Sections["localeSettings"] as MyAppLocaleSettingsSection;
        }
    }
}

public class MyAppLocaleSettingsSection: ConfigurationSection
{
    [ConfigurationProperty("longDateFormat", DefaultValue = "yyyy-MM-dd HH:mm")]
    public string LongDateFormat
    {
        get
        {
            return this["longDateFormat"] as string;
        }
    }
}

【问题讨论】:

    标签: .net asp.net web-config


    【解决方案1】:

    您尝试做的几乎所有事情都应该是可行的,并且应该没问题且合法 - 我猜您一定遗漏了一些小东西。我唯一不确定的是节组是否可以拥有自己的属性 - 它们可能被设计为只是节的容器,然后在其中包含实际的配置数据......

    要访问 web.config,您还应该尝试使用 WebConfigurationManager 而不是“直接”的 ConfigurationManager(用于 app.config 文件)。

    您能告诉我们您的MyApp.MyAppSectionHandlerMyApp.MyAppLocaleSettingsConfigurationSection 的代码吗?

    您是否在 CodeProject 上查看了 Jon Rista 关于 .NET 2.0 配置的三部分系列?这是关于如何使用和扩展 .NET 配置系统的极好介绍 - 强烈推荐,而且确实最有用!

    如果您正在处理自定义配置部分,我还建议您查看Configuration Section Designer,这是一个 Visual Studio 插件,可让您直观地定义配置部分组和配置部分以及属性和他们在这些部分中的数据类型 - 非常节省时间和教育工具!

    更多的挖掘表明:

    • 您可能会在自定义配置节组上定义[ConfigurationProperty],但这些属性没有自动“后备存储”,而且我看不到任何方法可以连接到从配置文件加载 XML , 或者 - 所以我猜部分组真的只是容器
    • 您可以将节组相互嵌套,但叶级别必须是配置节,并且只能嵌套在配置节组中。

    马克

    【讨论】:

    • 谢谢,我已经粘贴了代码。我不确定 sectionGroups 是否可以有属性,但我看到像 这样的东西可以做到,但当然它们可能很特别。我会查看 machine.config 和反射器,看看他们是否有一些见解。
    • 是的,看起来没有任何内置机制可以在部分组上具有属性....
    • 确实,在检查了几乎所有具有属性的类型之后,它们都是Sections 而不是SectionGroups。他们使用 Collections 等进行嵌套(编译是一个节,而汇编是该节内的编译,而不是 SectionGroup 中的一个节)。哦,好吧,这至少让整个事情变得更容易了。感谢您的链接。
    猜你喜欢
    • 2012-09-21
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    • 2017-02-12
    相关资源
    最近更新 更多