如何通过 C# 检索 Web.Config 中的键的值
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。在这方面的任何帮助将不胜感激。
你们很亲近;你想将 ConfigurationManager.GetSection("domainSource")
转换为 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"]; }
}
}
用法
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.
}
}
我正在通过 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。在这方面的任何帮助将不胜感激。
你们很亲近;你想将 ConfigurationManager.GetSection("domainSource")
转换为 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"]; }
}
}
用法
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.
}
}