2016-10-12 21 views
0

ASP.NET MVC4アプリケーションでは、web.configファイルからリストを読み込む必要があります。web.configでリストを定義および列挙する方法

それは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> 

を生成し実行している

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

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

関連する問題