C# 是否有任何方法可以使用AppSettings中的键访问custom config节的值?
我的web.config中有一个自定义的配置部分,如下所示:C# 是否有任何方法可以使用AppSettings中的键访问custom config节的值?,c#,asp.net,web-config,custom-configuration,C#,Asp.net,Web Config,Custom Configuration,我的web.config中有一个自定义的配置部分,如下所示: <configSection> <section name="CustomConfig" type="ConfigSectionRoot" allowLocation="true" allowDefinition="Everywhere"/> </configSection> <CustomConfig> <Co
<configSection>
<section name="CustomConfig" type="ConfigSectionRoot" allowLocation="true" allowDefinition="Everywhere"/>
</configSection>
<CustomConfig>
<ConfigRoot>
<add key="DataBase" value="CouchDB"/>
<add key="FrontEnd" value="Asp.Net"/>
<add key="AppName" value="Virtual WorkPlace"/>
</ConfigRoot>
</CustomConfig>
<AppSettings>
<add key="DataBase" value="CouchDB"/>
</AppSettings>
public class ConfigSectionRoot:ConfigurationSection
{
[ConfigurationProperty("key", DefaultValue = "", IsKey = true, IsRequired = true)]
public string Key
{
get
{
return ((string)(base["key"]));
}
set
{
base["key"] = value;
}
}
[ConfigurationProperty("value", DefaultValue = "", IsKey = false, IsRequired = false)]
public string Value
{
get
{
return ((string)(base["value"]));
}
set
{
base["value"] = value;
}
}
}
如果我使用AppSettings而不是自定义配置,我可以像这样访问它:
string results= ConfigurationManager.AppSettings["Database"];
// results wil contain "CouchDB"
在自定义配置部分有没有办法实现同样的功能???请帮助我。NET配置框架提供了表示元素列表的类。在上面的示例中,ConfigurationElementCollection的实现由ConfigRootXML元素表示。集合应具有类型为“ConfigSectionRoot”的子元素。ConfigSectionRoot类应从ConfigurationElement继承,而不是从Configuration节继承 必须创建一个单独的类来表示CustomConfig xml元素。此类是配置的根,必须从ConfigurationSection继承
public class CustomConfigConfigurationSection : ConfigurationSection
{
public static CustomConfigConfigurationSection Section
{
get
{
return ConfigurationManager.GetSection("customConfig") as CustomConfigConfigurationSection;
}
}
public ConfigConfigurationElementCollection ConfigRoot
{
get
{
return this["configRoot"] as ConfigConfigurationElementCollection;
}
}
}
public class ConfigConfigurationElement : ConfigurationElement
{
[ConfigurationProperty("key")]
public string Key
{
get
{
return (string)this["key"];
}
}
[ConfigurationProperty("value")]
public string Value
{
get
{
return (string)this["value"];
}
}
}
public class ConfigConfigurationElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ConfigConfigurationElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ConfigConfigurationElement)element).Key;
}
// Slight hack to look up the direct value property of the ConfigConfigurationElement from the collection indexer
public new string this[string key]
{
get
{
return (base[key] as ConfigConfigurationElement).Value;//I m getting the error in this line
}
}
}
直接使用:
var section = CustomConfigConfigurationSection.Section;
var value = section.ConfigRoot["key"];
NameValueSectionHandler
如果您的配置不需要超过一个键值存储,我会选择一个
SingleTagSectionHandler
您也可以通过SingleTagSection实现同样的功能:
<section name="customConfig" type="System.Configuration.SingleTagSectionHandler"/>
<!-- ... -->
<customConfig database="CouchDB" frontEnd="Asp.Net" appName="Virtual Workplace" />
您应该从ConfigurationSection派生,而不是ConfigurationElement。看看这个。@UfukHacıoğulları我已经改变了,我可以访问它。但我的问题是,我可以像在AppSetting中那样使用密钥访问值吗?我不这么认为。ConfigurationManager.AppSetting应检查@UfukHacıoğullarıFine.内的元素。。有类似的东西吗..我们可以使用CustomConfig.ConfigRoot[“Database”]之类的东西来获取值..您可以在ConfigSectionRoot类中实现索引器。谢谢您的帮助。。我已经按你说的做了。。但是我得到了一个“System.NullReferenceException:对象引用未设置为对象的实例”。从何处获得此异常?返回(base[key]as configurationElement)。值;谢谢你的帮助。。我没有得到预期的结果,我尝试了你的代码。。我做了一点小小的改动,然后就成功了。。请回顾一下。很抱歉,这是我所知道的和我能很快在网上找到的复制粘贴工作。没有通过测试,但你解决得恰到好处,tnx;)
var customConfig = (System.Collections.Specialized.NameValueCollection)System.Configuration.ConfigurationManager.GetSection("customConfig");//i have changed like this and it worked fine
var database = customConfig["DataBase"];
<section name="customConfig" type="System.Configuration.SingleTagSectionHandler"/>
<!-- ... -->
<customConfig database="CouchDB" frontEnd="Asp.Net" appName="Virtual Workplace" />
var customConfig = (System.Collections.Hashtable) System.Configuration.ConfigurationManager.GetConfig("customConfig");
var database = customConfig["database"];