简体   繁体   中英

How to retrieve the value for the Key in the Web.Config via C#

I am building a Webservice via VS2012 and need to fetch some data from the Web.Config file based on the dynamic data I would get from an Stored Procedure.

Code block in Web.Config

  <domainSource>
     <domainIDs>
         <add name="0" source="170" />
         <add name="1" source="171" />
         <add name="2" source="172" />
         <add name="3" source="173" />
         <add name="12" source="174" />
     </domainIDs>
  </domainSource>

Source code in DomainConfiguration.cs as below

public class DomainElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }

        [ConfigurationProperty("source", IsRequired = true, DefaultValue = "170")]
        public string Source
        {
            get { return (string)this["source"]; }
            set { this["source"] = value; }
        }
    }

    [ConfigurationCollection(typeof(DomainElement))]
    public class DomainCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new DomainElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((DomainElement)element).Name;
        }
    }

    public class DomainSection : ConfigurationSection
    {
        [ConfigurationProperty("domainIDs", IsDefaultCollection = true)]
        public DomainCollection DomainID
        {
            get { return (DomainCollection)this["domainIDs"]; }
            set { this["domainIDs"] = value; }
        }
    }

Sourcecode in the main cs file from where I wish to retrieve the data from the Web.Config

vDomainID = new B2B().Domain(SourceID, Subject);

DomainCollection collection;

collection = ConfigurationManager.GetSection("domainSource") as DomainCollection;
vDomainSource = collection[vDomainID];

This is where I am stuck vDomainID would be a value 0/1/2/3/12, based on this value I need to fetch its respective Source from the Web.Config . Any help on this aspect would be really appreciated.

You are very close; you want to cast ConfigurationManager.GetSection("domainSource") to DomainSection (Not DomainCollection) .

public class DomainElement : ConfigurationElement
{
    [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
    public string Name
    {
        get { return (string)this["name"]; }
        set { this["name"] = value; }
    }

    [ConfigurationProperty("source", IsRequired = true, DefaultValue = "170")]
    public string Source
    {
        get { return (string)this["source"]; }
        set { this["source"] = value; }
    }
}

[ConfigurationCollection(typeof(DomainElement))]
public class DomainCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new DomainElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((DomainElement)element).Name;
    }
}

public class DomainSection : ConfigurationSection
{
    [ConfigurationProperty("domainIDs", IsDefaultCollection = true)]
    public virtual DomainCollection domainIDs
    {
        get { return (DomainCollection)this["domainIDs"]; }
    }
}

Usage

string vDomainID = "0";
var domainSource = ConfigurationManager.GetSection("domainSource") as DomainSection;
var domainIDs = domainSource.domainIDs;
foreach (DomainElement item in domainIDs)
{
    if (item.Name.Equals(vDomainID))
    {
        // Do something.
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM