2012-12-21 5 views
6

나는 인식 할 수없는 구성 섹션

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

사용자 정의 구성 섹션 처리기
<configSections> 
    </configSections> 
    <Tabs> 
    <Tab name="Dashboard" visibility="true" /> 
    <Tab name="VirtualMachineRequest" visibility="true" /> 
    <Tab name="SoftwareRequest" visibility="true" /> 
    </Tabs> 

아래의 응용 프로그램 던져 예외를 인식 할 수없는 구성 섹션 탭을 실행하는 동안 같은 사용자 지정 구성 섹션을 만들었습니다. 이 문제를 해결하는 방법

+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

wrking이 아닙니다. – JEMI

+0

i이 섹션 이름을 " 탭 "type ="EDaaS.Web.Helper.CustomConfigurationHandler, EDaaS.Web "/> – JEMI

+0

오류가 발생합니까? –

관련 문제