如何通过C#检索Web.Config中Key的值

时间:2016-11-21 21:35:40

标签: c# asp.net web-services web-config

我正在通过VS2012构建Web服务,需要根据我从存储过程中获得的动态数据从Web.Config文件中获取一些数据。

Web.Config中的代码块

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

DomainConfiguration.cs中的源代码如下

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

DomainCollection collection;

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

主要cs文件中的源代码,我希望从Web.Config中检索数据

  $(document).ready(function() {
          ko.applyBindings(mymodel);
          $('#asyncoperation').click(function() {
              mymodel.content("<div>async operation content</div>");
              });

          });

这就是我被困的地方 vDomainID 将是一个值0/1/2/3/12,基于此值我需要从 获取其各自的来源Web.Config中 即可。在这方面的任何帮助将非常感激。

1 个答案:

答案 0 :(得分:0)

你很亲密;您想将ConfigurationManager.GetSection("domainSource")投射到DomainSection (非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.
    }
}