自定义 ConfigurationSection 在 web.config 中抛出无法识别的属性

Custom ConfigurationSection throws Unrecognized attribute in web.config

我正在研究并替换应用程序中以前的配置策略,这样它更有意义,但它在应用程序加载时抛出 "unrecognized attribute" 异常。

这是我的配置部分

using System.Configuration;

namespace MyApplication
{
    public class MyConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("defaultPageIndex", DefaultValue = 1)]
        [IntegerValidator(MinValue = 1)]
        public int DefaultPageIndex
        {
            get { return (int)this["defaultPageIndex"]; }
            set { this["defaultPageIndex"] = value; }
        }
    }
}

这是 web.config...

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="mySettingsGroup">
            <section name="mySettings" type="MyApplication.MyConfigurationSection" allowLocation="true" allowDefinition="Everywhere" />
        </sectionGroup>
    </configSections>
    <mySettingsGroup defaultPageIndex="1">
    </mySettingsGroup>
</configuration>

错误是Config Error Unrecognized attribute 'defaultPageIndex'

Config Source:
   27: 
   28:     <mySettingsGroup defaultPageIndex="1">
   29:     </mySettingsGroup>

我敢肯定这是我没看到的东西。

朋友指出,群里的版块不应该是这样的...

<mySettingsGroup defaultPageIndex="1">
</mySettingsGroup>

而是像这样...

<mySettingsGroup >
    <mySettings defaultPageIndex="5"/>
</mySettingsGroup>