无法识别的配置节 [英] Unrecognized configuration section

查看:147
本文介绍了无法识别的配置节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了如下图所示的自定义配置节

I have created a custom configuration section like below

 <configSections>
  </configSections>
  <Tabs>
    <Tab name="Dashboard" visibility="true" />
    <Tab name="VirtualMachineRequest" visibility="true" />
    <Tab name="SoftwareRequest" visibility="true" />
  </Tabs>

自定义配置节处理程序

 namespace EDaaS.Web.Helper
    {
        public class CustomConfigurationHandler : ConfigurationSection
        {
            [ConfigurationProperty("visibility", DefaultValue = "true", IsRequired = false)]
            public Boolean Visibility
            {
                get
                {
                    return (Boolean)this["visibility"];
                }
                set
                {
                    this["visibility"] = value;
                }
            }
        }
    }

在运行应用程序抛出异常的无法识别的配置栏目标签。如何解决这个

While running the application throw exception Unrecognized configuration section Tabs. How to resolve this

推荐答案

您需要编写的 配置处理器 来解析该自定义栏目。然后在你的config文件中注册该自定义处理程序:

You need to write a configuration handler to parse this custom section. And then register this custom handler in your config file:

<configSections>
    <section name="mySection" type="MyNamespace.MySection, MyAssembly" />
</configSections>

<mySection>
    <Tabs>
        <Tab name="one" visibility="true"/>
        <Tab name="two" visibility="true"/>
    </Tabs>
</mySection>

现在让我们来定义相应的配置部分:

Now let's define the corresponding config section:

public class MySection : ConfigurationSection
{
    [ConfigurationProperty("Tabs", Options = ConfigurationPropertyOptions.IsRequired)]
    public TabsCollection Tabs
    {
        get
        {
            return (TabsCollection)this["Tabs"];
        }
    }
}

[ConfigurationCollection(typeof(TabElement), AddItemName = "Tab")]
public class TabsCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new TabElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        if (element == null)
        {
            throw new ArgumentNullException("element");
        }
        return ((TabElement)element).Name;
    }
}

public class TabElement : ConfigurationElement
{
    [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
    public string Name
    {
        get { return (string)base["name"]; }
    }

    [ConfigurationProperty("visibility")]
    public bool Visibility
    {
        get { return (bool)base["visibility"]; }
    }
}

现在你可以访问的设置:

and now you could access the settings:

var mySection = (MySection)ConfigurationManager.GetSection("mySection");

这篇关于无法识别的配置节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆