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

时间:2017-02-01 16:47:08

标签: asp.net asp.net-mvc system.configuration

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

这是我的配置部分

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>

我确信这件事是愚蠢的,我只是没有看到。

1 个答案:

答案 0 :(得分:0)

正如朋友所指出的那样,小组中的部分不应该像这样......

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

但是喜欢这样......

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