0

我正在尝试从 Web.config 文件中的自定义配置中检索值,同时在 Visual Studio 2012 中运行我的一个 Web 项目。

当我尝试执行此代码时:

classes.Globals.ASPNET2Configuration configInfoRepresentativeAddress =  
              (classes.Globals.ASPNET2Configuration)ConfigurationManager.GetSection("customConfig/RepresentativeAddresses");

我收到此错误:

错误:为 customConfig/RepresentativeAddresses 创建配置节处理程序时出错:无法加载类型“pri001___P_Website.classes.Globals.ASPNET2Configuration”。(C:\[pathToProjectFiles]\web.config 第 9 行) 请注意:默认命名空间中的“P”代表其他内容 - 我删除了此帖子的实际值

我很确定我的问题在于Web.config 文件节点中的type属性。section我为此研究并尝试了几种不同的值,但都失败了。包含配置类的 Globals.cs 文件位于名为“classes”的文件夹中。这是目录结构的一部分,有助于澄清:

项目目录树

这是标记:

 <configuration>
      <configSections>
    <sectionGroup name="customConfig">
      <section name="RepresentativeAddresses" 
               requirePermission="false" 
               type="pri001___P_Website.classes.Globals.ASPNET2Configuration" 
               allowDefinition="Everywhere" 
               allowLocation="true" />
    </sectionGroup>
  </configSections>
  <customConfig>
    <RepresentativeAddresses>
      <Representatives>
        <add representativeName="biff" eMailAddress="biff@foo.com"/>
        <add representativeName="dick" eMailAddress="dick@foo.com"/>
        <add representativeName="rod" eMailAddress="rod@foo.com"/>
      </Representatives>
    </RepresentativeAddresses>
  </customConfig>

这是我的配置类的代码:

 namespace pri001___P_Website.classes
{
    public static class Globals
    {      
        public class ASPNET2Configuration_Representative : ConfigurationElement
        {
            [ConfigurationProperty("representativeName")]
            public string representativeName
            {
                get { return this["representativeName"] as string; }
            }
            [ConfigurationProperty("eMailAddress")]
            public string eMailAddress
            {
                get { return this["eMailAddress"] as string; }
            }
        }

        public class ASPNET2Configuration_RepresentativeCollection : ConfigurationElementCollection
        {
            public ASPNET2Configuration_Representative this[int index]
            {
                get
                {
                    return base.BaseGet(index) as ASPNET2Configuration_Representative;
                }
                set
                {
                    if (base.BaseGet(index) != null)
                        base.BaseRemoveAt(index);

                    this.BaseAdd(index, value);
                }
            }

            protected override ConfigurationElement CreateNewElement()
            {
                return new ASPNET2Configuration_Representative();
            }

            protected override object GetElementKey(ConfigurationElement element)
            {
                return ((ASPNET2Configuration_Representative)element).representativeName;
            }
        }

        public class ASPNET2Configuration : ConfigurationSection
        {
            public static ASPNET2Configuration GetConfig()
            {
                return ConfigurationManager.GetSection("customConfig/RepresentativeAddresses") as ASPNET2Configuration;
            }

            [ConfigurationProperty("Representatives")]
            public ASPNET2Configuration_RepresentativeCollection Representatives
            {
                get { return this["Reprensentatives"] as ASPNET2Configuration_RepresentativeCollection; }
            }
        }
    }
}

type房产的价值实际上是我的问题,还是有其他问题?

4

0 回答 0