【问题标题】:Unrecognized configuration section无法识别的配置部分
【发布时间】:2012-12-21 07:19:24
【问题描述】:

我创建了一个自定义配置部分,如下所示

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

运行应用程序时抛出异常无法识别的配置部分选项卡。如何解决?

【问题讨论】:

  • 你能展示你的sectionGroup配置吗?
  • 你在 configSections 中有什么选项卡吗?
  • 我已经添加了这样

标签: asp.net .net asp.net-mvc


【解决方案1】:

您需要编写configuration handler 来解析此自定义部分。然后在你的配置文件中注册这个自定义处理程序:

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

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

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

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

现在您可以访问设置了:

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

【讨论】:

  • 我在配置部分中添加了一个部分,如下所示
  • 我得到了运行时执行
猜你喜欢
  • 2016-12-17
  • 1970-01-01
  • 2011-01-07
  • 1970-01-01
  • 1970-01-01
  • 2015-10-27
  • 2015-05-28
  • 1970-01-01
  • 2011-12-28
相关资源
最近更新 更多