Multiple values for a single config key

后端 未结 10 2083
被撕碎了的回忆
被撕碎了的回忆 2020-12-05 01:37

I\'m trying to use ConfigurationManager.AppSettings.GetValues() to retrieve multiple configuration values for a single key, but I\'m always receiving an array o

10条回答
  •  猫巷女王i
    2020-12-05 02:28

    Here is full solution: code in aspx.cs

    namespace HelloWorld
    {
        public partial class _Default : Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                UrlRetrieverSection UrlAddresses = (UrlRetrieverSection)ConfigurationManager.GetSection("urlAddresses");
            }
        }
    
        public class UrlRetrieverSection : ConfigurationSection
        {
            [ConfigurationProperty("", IsDefaultCollection = true,IsRequired =true)]
            public UrlCollection UrlAddresses
            {
                get
                {
                    return (UrlCollection)this[""];
                }
                set
                {
                    this[""] = value;
                }
            }
        }
    
    
        public class UrlCollection : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new UrlElement();
            }
            protected override object GetElementKey(ConfigurationElement element)
            {
                return ((UrlElement)element).Name;
            }
        }
    
        public class UrlElement : ConfigurationElement
        {
            [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
            public string Name
            {
                get
                {
                    return (string)this["name"];
                }
                set
                {
                    this["name"] = value;
                }
            }
    
            [ConfigurationProperty("url", IsRequired = true)]
            public string Url
            {
                get
                {
                    return (string)this["url"];
                }
                set
                {
                    this["url"] = value;
                }
            }
    
        }
    }
    

    And in web config

    
       

提交回复
热议问题