【问题标题】:How to define and enumerate list in web.config如何在 web.config 中定义和枚举列表
【发布时间】:2016-10-13 14:07:53
【问题描述】:

ASP.NET MVC4 应用程序需要从 web.config 文件中读取列表。

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>

枚举它的代码是:

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

运行它会产生错误

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 可以包含多个连接元素?

节定义为:

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

【问题讨论】:

    标签: c# asp.net asp.net-mvc-4 web-config configurationmanager


    【解决方案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; } }
    }
    

    【讨论】:

    • 并调用 (WebConfigurationManager.GetSection(ConnectionGroupConfigurationSection.NodeName) as ConnectionGroupConfigurationSection).ConnectionGroup.Cast()
    猜你喜欢
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 2015-02-20
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    相关资源
    最近更新 更多