6

我的 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"

有没有办法在自定义配置部分实现同样的事情???请帮帮我

4

2 回答 2

11

NameValueSectionHandler

If your configuration doesn't need to be more than a key-value store, I'd go for a 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>

You can then read it out, just like the 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

You could also achieve the same with a SingleTagSection:

<section name="customConfig" type="System.Configuration.SingleTagSectionHandler"/>
<!-- ... -->
<customConfig database="CouchDB" frontEnd="Asp.Net" appName="Virtual Workplace" />

And then query it with:

var customConfig = (System.Collections.Hashtable) System.Configuration.ConfigurationManager.GetConfig("customConfig");
var database = customConfig["database"];
于 2012-10-15T22:10:44.787 回答
0

.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"];
于 2012-10-15T20:41:09.963 回答