簡體   English   中英

嘗試在Web.config中創建自定義部分

[英]Try to create a custom section in Web.config

我試圖在我的web.config中創建一個自定義部分。 我的web.config中有一個自定義類和一個聲明部分。

但是,當我運行代碼時,卻收到此錯誤“ ...不繼承自System.Configuration.IConfigurationSectionHandler'。”

MSDN文檔指出此接口現在已被棄用。
我不明白正在發生什么寒冷。
僅供參考,我以此為依據( http://blogs.perficient.com/microsoft/2017/01/4-easy-steps-to-custom-sections-in-web-config/作為創建我的自定義部分的指南。

Here is my web.config    



<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <section name ="BDKConfigs" type="SPS.CardDecryption.Services.BDKConfigs" allowLocation="true" allowDefinition="Everywhere" />
  </configSections>

Here is my custom section in the web.config      



 <BDKConfigs>
        <!--collection-->
        <BDK>
          <!--elements-->
          <add name="IDTechPublicBDK" value="0123456789ABCDEFFEDCBA9876543210"/>
          <add name="IDTechProdBDK" value=""/>
         </BDK>
    </BDKConfigs>

this is my custom class      


using System.Configuration;

namespace myNameSpace
{
    //This class reads the defined config section (if available) and stores it locally in the static _Config variable.  

    public class BDKConfigs 
    {
        public static BDKConfigsSection _Config = ConfigurationManager.GetSection("BDKConfigs") as BDKConfigsSection;

        public static BDKElementCollection GetBDKGroups()
        {
            return _Config.BDKConfigs;
        }


    }

     public class BDKConfigsSection : ConfigurationSection
    {
        //Decorate the property with the tag for your collection.
        [ConfigurationProperty("BDK")]
        public BDKElementCollection BDKConfigs
        {
            get { return (BDKElementCollection)this["BDK"]; }
        }
    }

    //Extend the ConfigurationElementCollection class.
    //Decorate the class with the class that represents a single element in the collection. 
    [ConfigurationCollection(typeof(BDKElement))]
    public class BDKElementCollection : ConfigurationElementCollection
    {
        public BDKElement this[int index]
        {
            get { return (BDKElement)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);

                BaseAdd(index, value);
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new BDKElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((BDKElement)element).Name;
        }
    }

    //Extend the ConfigurationElement class.  This class represents a single element in the collection. 
    //Create a property for each xml attribute in your element.  
    //Decorate each property with the ConfigurationProperty decorator.  See MSDN for all available options.
    public class BDKElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }

        [ConfigurationProperty("queryString", IsRequired = false)]
        public string Value
        {
            get { return (string)this["Value"]; }
            set { this["Value"] = value; }
        }

    }
}

問題是我在web.config中指定了錯誤的類型。 當我將web.config更改為

type =“ SPS.CardDecryption.Services.BDKConfigsSection,就像一個魅力。

我已經更新了答案:

 public class BDKConfigsSection : ConfigurationSection
{
    [ConfigurationProperty("BDK")]
    public BDKElementCollection BDKConfigs
    {
        get { return (BDKElementCollection) base["BDK"]; }
    }
}

[ConfigurationCollection(typeof(BDKElement))]
public class BDKElementCollection : ConfigurationElementCollection
{
    public BDKElement this[int index]
    {
        get { return (BDKElement)BaseGet(index); }
        set
        {
            if (BaseGet(index) != null)
                BaseRemoveAt(index);

            BaseAdd(index, value);
        }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new BDKElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((BDKElement)element).Name;
    }
    public override ConfigurationElementCollectionType CollectionType
    {
        get { return ConfigurationElementCollectionType.BasicMapAlternate; }
    }
    protected override string ElementName => "BDKItem";
}

public class BDKElement : ConfigurationElement
{
    [ConfigurationProperty("name", IsRequired = true)]
    public string Name
    {
        get { return (string)this["name"]; }
        set { this["name"] = value; }
    }

    [ConfigurationProperty("queryString", IsRequired = false)]
    public string Value
    {
        get { return (string)this["queryString"]; }
        set { this["queryString"] = value; }
    }

}

那個工作正常:

  <BDKConfigsSection>
<BDK>
  <BDKItem name="IDTechPublicBDK" queryString="0123456789ABCDEFFEDCBA9876543210"/>
  <BDKItem name="IDTechProdBDK" queryString=""/>
</BDK> </BDKConfigsSection>

暫無
暫無

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

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