【问题标题】:Is there any way to access the value of custom config section using the key as in AppSettings?有没有办法使用 AppSettings 中的键访问自定义配置部分的值?
【发布时间】:2012-10-03 02:55:10
【问题描述】:

我的 web.config 中有一个自定义配置部分,如下所示:

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

我的 ConfigSectionRoot.cs 是这样的:

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"

有没有什么方法可以在自定义配置部分实现同样的效果???请帮帮我

【问题讨论】:

  • 您应该派生自 ConfigurationSection,而不是 ConfigurationElement。看看这个example
  • @UfukHacıoğulları 我已更改,我可以访问它。但我的问题是我可以使用 AppSettings 中的键访问值吗
  • 我不这么认为。 ConfigurationManager.AppSetting 可能会检查 中的元素
  • @UfukHacıoğulları 很好.. 有没有类似的东西.. 我们可以使用 CustomConfig.ConfigRoot["Database"] 之类的东西来获取值..
  • 你可以在你的 ConfigSectionRoot 类中实现一个索引器。

标签: c# asp.net web-config custom-configuration


【解决方案1】:

NameValueSectionHandler

如果您的配置不需要超过键值存储,我会选择NameValueSectionHandler

<section name="customConfig" type="System.Configuration.NameValueSectionHandler"/>
<!-- ... -->
<customConfig>
  <add key="DataBase" value="CouchDB" />
  <add key="FrontEnd" value="Asp.Net" />
  <add key="AppName" value="Virtual WorkPlace" />
</customConfig>

然后你可以读出它,就像 appSettings 一样:

var customConfig = (System.Collections.Specialized.NameValueCollection)System.Configuration.ConfigurationManager.GetSection("customConfig");//i have changed like this and it worked fine
var database = customConfig["DataBase"];

SingleTagSectionHandler

您也可以使用 SingleTagSection 实现同样的效果:

<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"];

【讨论】:

  • 感谢您的帮助..我尝试了您的代码时没有得到预期的结果..我只是做了一些小改动然后它就起作用了..请检查它..
  • 很抱歉,这是我所知道的复制粘贴工作,并且可以在网上快速找到。没有测试通过,但你解决得恰到好处,tnx ;)
【解决方案2】:

.NET 配置框架提供ConfigurationElementCollection 类来表示元素列表。在上面的示例中,您的 ConfigurationElementCollection 实现由 ConfigRoot xml 元素表示。该集合应具有“ConfigSectionRoot”类型的子元素。 ConfigSectionRoot 类应该继承自 ConfigurationElement,而不是 Configuration Section。

您必须创建一个单独的类来表示 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"];

【讨论】:

  • 感谢您的帮助..我已经按照您的说明进行了尝试..但我得到一个“System.NullReferenceException:对象引用未设置为对象的实例。”
  • 你从哪里得到这个异常?
  • return (base[key] as ConfigConfigurationElement).Value;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
  • 2012-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多