簡體   English   中英

如何通過C#在Web.Config中檢索Key的值

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

我正在通過VS2012構建Web服務,並且需要根據從存儲過程中獲取的動態數據從Web.Config文件中獲取一些數據。

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>

DomainConfiguration.cs中的源代碼如下

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; }
        }
    }

我要從中從Web.Config檢索數據的主要CS文件中的源代碼

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

DomainCollection collection;

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

在這里我被卡住了vDomainID將是一個值0/1/2/3/12,基於該值,我需要從Web.Config中獲取其各自的Source。 在這方面的任何幫助將不勝感激。

你很親密 您想將ConfigurationManager.GetSection("domainSource") DomainSectionDomainSection (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"]; }
    }
}

用法

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.
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM