即使IsKey为false,也已添加条目错误

时间:2019-01-15 13:49:24

标签: c# asp.net-mvc web-config configurationmanager

尝试为Entry has already been added使用相同的标题时出现SubModule错误。即使IsKey = false发生了,但仍不允许输入重复的Upload作为标题

如何解决此问题?

这是我的web.config

<SiteModules>
    <Modules>
      <MainModule title="MyUpload">
        <SubModule title="Upload" page="/script/upload1" groups="group1" display="true" type="maker"></SubModule>
        <SubModule title="Upload" page="/script/upload2" groups="group2" display="true" type="checker"></SubModule>
        <SubModule title="SomeTitle1" page="/script/upload3" groups="group1" display="false"></SubModule>
      </MainModule>
    </Modules>
  </SiteModules>

这是我的课程

namespace MyClasses
{
    public class SiteModules : ConfigurationSection
    {
        [ConfigurationProperty("Modules", IsDefaultCollection = false)]
        public Modules Modules
        {
            get
            {
                Modules modulesConfigElement = (Modules)base["Modules"];
                return modulesConfigElement;
            }
        }
    }

    public class Modules : ConfigurationElementCollection
    {
        public Modules()
        {
            AddElementName = "MainModule";
        }
        protected override ConfigurationElement CreateNewElement()
        {
            return new MainModule();
        }
        protected override Object GetElementKey(ConfigurationElement element)
        {
            return ((MainModule)element).Title;
        }
    }

    public class MainModule : ConfigurationElementCollection
    {
        public MainModule()
        {
            AddElementName = "SubModule";
        }
        [ConfigurationProperty("title", IsRequired = true, IsKey = false)]
        public string Title
        {
            get
            {
                return (string)this["title"];
            }
            set
            {
                this["title"] = value;
            }
        }
        protected override ConfigurationElement CreateNewElement()
        {
            return new SubModule();
        }
        protected override Object GetElementKey(ConfigurationElement element)
        {
            return ((SubModule)element).Title;
        }
    }

    public class SubModule : ConfigurationElement
    {
        [ConfigurationProperty("title", IsRequired = true, IsKey = false)]
        public string Title
        {
            get
            {
                return (string)this["title"];
            }
            set
            {
                this["title"] = value;
            }
        }

        [ConfigurationProperty("page", IsRequired = true)]
        public string Page
        {
            get
            {
                return (string)this["page"];
            }
            set
            {
                this["page"] = value;
            }
        }

        [ConfigurationProperty("groups", IsRequired = true)]
        public string Groups
        {
            get
            {
                return (string)this["groups"];
            }
            set
            {
                this["groups"] = value;
            }
        }

        [ConfigurationProperty("display", IsRequired = true)]
        public string Display
        {
            get
            {
                return (string)this["display"];
            }
            set
            {
                this["display"] = value;
            }
        }

        [ConfigurationProperty("type", IsRequired = false)]
        public string Type
        {
            get
            {
                return (string)this["type"];
            }
            set
            {
                this["type"] = value;
            }
        }
    }
}

引发错误的代码是:

SiteModules siteModules = (SiteModules)ConfigurationManager.GetSection("SiteModules");

1 个答案:

答案 0 :(得分:0)

与IsKey属性无关的异常。查看重写的方法GetelementKey。
在给定的情况下,它返回Title ..这不是唯一的。

    protected override Object GetElementKey(ConfigurationElement element)
    {
        return ((MainModule)element).Title;
    }