自定义配置文件C#根据用户的角色重定向用户

自定义配置文件C#根据用户的角色重定向用户,c#,asp.net,asp.net-mvc,asp.net-mvc-4,web-config,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Web Config,我试图编写一个正确的配置文件,但我认为我混淆了几个定义。最终目的是根据用户的角色将其重定向到正确的页面 问题: 具有多个标记的嵌套元素不是 ConfigurationElementCollection 单个元素不是一个 配置元素 如果这是正确的,下面的代码中哪一个是我的错误 以下是我的例子,我在我的网络配置中写道: <configSections> <section name="adminSecurityConfig" type="Web.Portal.Au

我试图编写一个正确的配置文件,但我认为我混淆了几个定义。最终目的是根据用户的角色将其重定向到正确的页面

问题:

  • 具有多个标记的嵌套元素不是 ConfigurationElementCollection
  • 单个元素不是一个 配置元素
如果这是正确的,下面的代码中哪一个是我的错误

以下是我的例子,我在我的网络配置中写道:

<configSections>
        <section name="adminSecurityConfig" type="Web.Portal.Authentication.ViewModels.RoleConfiguration.AdminSecuritySection" />
</configSections>

<!--Roles for Web Portals and a link to an external file-->
<adminSecurityConfig configSource="AdminSecurity.xml" />

我还有几个类可以访问xml中的元素。我使用->来表示下面字符串上的包含

AdminSecuritySection->RoleCollection->RoleElement(具有角色的name属性)->UrleElementCollection->UrleElement(具有重定向的源)

以下是课程:

  • 行政及保安组

    public class AdminSecuritySection : ConfigurationSection
    {
        [ConfigurationProperty("role", IsDefaultCollection = false)]
        ConfigurationCollection(typeof(RoleCollection))]
        public RoleCollection Roles
        {
            get
            {
                return this["role"] as RoleCollection ?? new RoleCollection();
            }
            set { this["role"] = value; }
        }
    }
    
  • 角色集合

    public class RoleCollection : ConfigurationElementCollection
    {
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }
    
        protected override ConfigurationElement CreateNewElement()
        {
            return new RoleElement();
        }
    
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((RoleElement)element).Name;
        }
    
        protected override string ElementName
        {
            get { return "role"; }
        }
    
        public void Add(RoleElement element)
        {
            base.BaseAdd(element);
        }
    
        public void Remove(RoleElement element)
        {
            base.BaseRemove(element);
        }
    
        public RoleElement this[string name]
        {
            get
            {
                foreach (var item in this)
                {
                    if ((item as RoleElement).Name == name)
                        return item as RoleElement;
                }
    
                return null;
            }
        }
    }
    
    public class UrlElementCollection : ConfigurationElementCollection
    {
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }
    
        protected override string ElementName
        {
            get
            {
                return "redirectUrl";
            }
        }
    
        protected override ConfigurationElement CreateNewElement()
        {
            return new UrlElement();
        }
    
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((UrlElement)element).Source;
        }
    
        public void Add(UrlElement element)
        {
            this.BaseAdd(element);
        }
    
        public void Remove(UrlElement element)
        {
            this.BaseRemove(element);
        }
    
        public UrlElement this[string source]
        {
            get
            {
                foreach (var item in this)
                {
                    if ((item as UrlElement).Source == source)
                        return item as UrlElement;
                }
    
                return null;
            }
        }
    }
    
  • RoleElement

    public class RoleElement : ConfigurationElement
    {
        [ConfigurationProperty("redirectUrl", IsDefaultCollection = false)]
        [ConfigurationCollection(typeof(UrlElementCollection))]
        public UrlElementCollection Urls
        {
            get { return this["redirectUrl"] as UrlElementCollection ?? new UrlElementCollection(); }
            set { this["redirectUrl"] = value; }
        }
    
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }
    }
    
  • UrleElementCollection

    public class RoleCollection : ConfigurationElementCollection
    {
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }
    
        protected override ConfigurationElement CreateNewElement()
        {
            return new RoleElement();
        }
    
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((RoleElement)element).Name;
        }
    
        protected override string ElementName
        {
            get { return "role"; }
        }
    
        public void Add(RoleElement element)
        {
            base.BaseAdd(element);
        }
    
        public void Remove(RoleElement element)
        {
            base.BaseRemove(element);
        }
    
        public RoleElement this[string name]
        {
            get
            {
                foreach (var item in this)
                {
                    if ((item as RoleElement).Name == name)
                        return item as RoleElement;
                }
    
                return null;
            }
        }
    }
    
    public class UrlElementCollection : ConfigurationElementCollection
    {
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }
    
        protected override string ElementName
        {
            get
            {
                return "redirectUrl";
            }
        }
    
        protected override ConfigurationElement CreateNewElement()
        {
            return new UrlElement();
        }
    
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((UrlElement)element).Source;
        }
    
        public void Add(UrlElement element)
        {
            this.BaseAdd(element);
        }
    
        public void Remove(UrlElement element)
        {
            this.BaseRemove(element);
        }
    
        public UrlElement this[string source]
        {
            get
            {
                foreach (var item in this)
                {
                    if ((item as UrlElement).Source == source)
                        return item as UrlElement;
                }
    
                return null;
            }
        }
    }
    
  • UrlElement

    public class UrlElement : ConfigurationElement
    {
        [ConfigurationProperty("src", IsRequired = true)]
        public string Source
        {
            get { return (string)this["src"]; }
            set { this["src"] = value; }
        }
    }
    
最后,当我尝试运行它时,会出现以下错误:


看起来您的RoleCollection和RoleElement具有相同的名称,即role。这可能令人困惑。当它遇到role标记时,它将被视为没有name属性的角色集合。将RoleCollection名称更改为roles和xml结构,如下所示

<?xml version="1.0" encoding="utf-8" ?>
  <adminSecurityConfig>
    <roles>
     <role name="Employee">
        <redirectUrl src="www.google.com"></redirectUrl>
        <redirectUrl src="www.yahoo.com"></redirectUrl>
        <redirectUrl src="www.amazon.com"></redirectUrl>
     </role>
     <role name="Member">
        <redirectUrl src="www.yahoo.com"></redirectUrl>
     </role>
     <role name="Group Administrator">
     </role>
   </roles>
  </adminSecurityConfig>


那么它不起作用了还是什么?属性名称不起作用看起来您的RoleCollection和RoleElement都有相同的名称,即role。这可能令人困惑。当它遇到role标记时,它被视为没有name属性的role集合。但是在代码中从未设置复数形式的name roles。实际上,如果我添加“unrecognedelement'roles'”这个链接可能会有帮助,那么就会抛出这个异常。