2012-12-21 59 views
6

我创建了一个自定义配置节像下面无法识别的配置节

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

运行应用程序时抛出异常无法识别的配置节标签。如何解决此问题

+0

可以显示你的sectionGroup配置? – dove

+0

如何显示? – JEMI

+0

你有什么东西在configSections的标签? – dove

回答

15

您需要编写一个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"); 
+0

它不是正在运行 – JEMI

+0

我已经在配置部分添加了一个部分,如

JEMI

+0

您是否收到错误消息? –

相关问题