2016-10-12 2 views
0

ASP.NET MVC4 응용 프로그램은 web.config 파일에서 목록을 읽어야합니다.web.config에서 목록을 정의하고 열거하는 방법

 IList<ConnectionSection> connection = 
       (IList<ConnectionSection>)ConfigurationManager.GetSection("connectionGroup"); 
     foreach (var s in connection) 
      Debug.WriteLine(s.Url); 

은 그렇게 그 Web.config의가 포함 할 수있는이 문제를 해결하는 방법 오류를

Parser Error Message: Sections must only appear once per config file. See the help topic <location> for exceptions. 

Source Error: 

Line 19: <connectionGroup> 
Line 20:  <connection url="site1" database="site1" /> 
Line 21:  <connection url="site2" database="site2" /> 
Line 22: </connectionGroup> 

를 생산 실행 :

Web.config의 그것을 열거 할

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 

    <configSections> 
    <sectionGroup name="connectionGroup"> 
     <section 
     name="connection" 
     type="Helpers.connectionSection" 
     allowLocation="true" 
     allowDefinition="Everywhere" 
     /> 
    </sectionGroup> 
    </configSections> 

    <connectionGroup> 
    <connection url="site1" database="db1" /> 
    <connection url="site2" database="db2" /> 
    </connectionGroup> 

코드입니다 포함 여러 연결 요소?

섹션으로 정의된다 :

namespace Helpers 
{ 
    public class ConnectionSection : ConfigurationSection 
    { 

     [ConfigurationProperty("url", IsRequired = true, IsKey = true)] 
     public string Url 
     { 
      get { return (string)this["url"]; } 
      set { this["url"] = value; } 
     } 

     [ConfigurationProperty("database", DefaultValue = "mydb", IsRequired = false)] 
     public string Database 
     { 
      get { return (string)this["database"]; } 
      set { this["database"] = value; } 
     } 
    } 
    } 

답변

1
public class ConnectionGroupConfigurationSection : ConfigurationSection 
{ 
    public const string NodeName = "connectionGroup"; 

    [ConfigurationProperty("", IsDefaultCollection=true)] 
    public ConnectionGroupConfigurationElementCollection ConnectionGroup 
    { 
     get { return (ConnectionGroupConfigurationElementCollection)this[""]; } 
    } 
} 

public class ConnectionGroupConfigurationElementCollection : ConfigurationElementCollection 
{ 
    protected override ConfigurationElement CreateNewElement() 
    { 
     return new ConnectionElement(); 
    } 

    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return (element as ConnectionElement).Id; 
    } 

    public override ConfigurationElementCollectionType CollectionType 
    { 
     get { return ConfigurationElementCollectionType.BasicMap; } 
    } 

    protected override string ElementName 
    { 
     get { return "connection"; } 
    } 
} 

public class ConnectionElement : ConfigurationElement 
{ 
    [ConfigurationProperty("url", IsRequired=true, IsKey=true)] 
    public string Url { get { return (string)this["url"]; } } 

    [ConfigurationProperty("database", DefaultValue = "mydb", IsRequired = false)] 
    public string Database { get { return (string)this["database"]; } set { this["database"] = value; } } 
} 
+0

및 호출 (ConnectionGroupConfigurationSection 같은 WebConfigurationManager.GetSection (ConnectionGroupConfigurationSection.NodeName)) .ConnectionGroup.Cast () – Nathan

관련 문제