繁体   English   中英

ASP.NET MVC 自定义配置 GetSection 返回 null

[英]ASP.NET MVC Custom configuration GetSection returns null

我正在使用 .net 框架 4.5 处理继承的 ASP.NET MVC 4 项目。

我们添加了一个新的配置节文件和相关的 class 文件,据我们所知(docs.Microsoft 和其他在线指南),它设置正确。

问题

ConfigurationManager.GetSection()返回 null。

根据文档,如果该部分不存在,则返回 null 。 对此进行故障排除很麻烦。

编码

该网站是一个 ASP.NET Web 应用程序。 属性 window 将程序集名称设置为 Client.Project.UI.Base(即已发布 bin 中的 DLL)。 这是用于配置类型 FQN 和 web.config 中的程序集的程序集名称。

注意:配置部分 SupportCaseConfiguration 最初位于一个单独的文件中,而 SupportTickets 部分只是指定了 configSource。 这已移至 web.config 以减少故障排除时潜在问题的数量。

web.config:

<configSections>    
  <!-- define type for new section -->
  <section name="SupportTickets" type="Client.Project.UI.Base.Infrastructure.Services.SupportCaseConfigurationSection, Client.Project.UI.Base"/>
</configSections>    

    <!-- new config section -->
    <SupportTickets>
      <SupportCaseConfiguration>
        <caseTypes>
          <add name="tenant.TestCase" label="Test Case" recipient="email_here" ccList="" bccList="" />
        </caseTypes>
      </SupportCaseConfiguration>    
    </SupportTickets>

SupportCaseConfiguration.cs:

namespace Client.Project.UI.Base.Infrastructure.Services
{
    using System.Configuration;

    //Extend the ConfigurationSection class.
    public class SupportCaseConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("caseTypes", IsDefaultCollection = true)]
        public CaseTypeElementCollection CaseTypes
        {
            get { return (CaseTypeElementCollection)this["caseTypes"]; }
        }
    }

    //Extend the ConfigurationElementCollection class.
    [ConfigurationCollection(typeof(CaseTypeElement))]
    public class CaseTypeElementCollection : ConfigurationElementCollection
    {
        public CaseTypeElement this[int index]
        {
            get { return (CaseTypeElement)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }
        }

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

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((CaseTypeElement)element).Name;
        }
    }

    //Extend the ConfigurationElement class.  This class represents a single element in the collection.
    public class CaseTypeElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }

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

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

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

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

在其他地方,获取新的配置数据:

SupportCaseConfigurationSection supportTicketsConfigurationSection = ConfigurationManager.GetSection("SupportCaseConfiguration") as SupportCaseConfigurationSection;

该站点正在本地发布,我可以附加一个调试器以确保使用最新版本的文件。 我可以在已发布的 web.config 中看到配置部分。

我一直在看这个,我再也看不出有什么不对劲了。 对我来说一切都很好...

任何想法、故障排除技巧,甚至指出我是布偶都会很有用。

干杯。

我只能假设问题与站点程序集中的配置类有关。

即使在尝试了在线示例,复制粘贴到项目中之后,它们甚至都不起作用。

一旦我将配置部分/元素类放在一个单独的项目中(从网站项目中移出),它就开始工作了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM