簡體   English   中英

有沒有辦法在AppSettings中使用密鑰訪問自定義配置部分的值?

[英]Is there any way to access the value of custom config section using the key as in AppSettings?

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

有沒有辦法在Customized Config部分實現相同的功能??? 請幫助我

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM