自定义配置节在 web.config 中引发无法识别的属性



我正在检查并替换应用程序中以前的配置策略,以便它更有意义,但它在应用程序加载时抛出"无法识别的属性"异常。

这是我的配置部分

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; }
        }
    }
}

这是网络配置...

<?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>

最新更新