简体   繁体   中英

ASP.NET MVC Custom configuration GetSection returns null

I'm working on an inherited ASP.NET MVC 4 project using .net framework 4.5.

We've added a new configuration section files and relevant class files and from what we can tell (docs.Microsoft and other online guides) it's set up correctly.

The Problem

ConfigurationManager.GetSection() returns null.

According to the docs this returns null if the section doesn't exist. Troubleshooting this has been troublesome.

The Code

The website is an ASP.NET Web Application. Properties window sets assembly name to Client.Project.UI.Base (which is the DLL in the published bin). This is the assembly name used for the config types FQN and assembly in web.config.

NB: the config section SupportCaseConfiguration was originally in a separate file and the SupportTickets section just specified the configSource. This has been moved into the web.config to reduce the number of potential issues while troubleshooting.

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

Elsewhere, getting new config data:

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

The site is being published locally, I can attach a debugger to ensure the latest versions of files are being used. I can see the config section in the published web.config.

I've been looking at this I can no longer see if anything is amiss. It all looks fine for me...

Any ideas, troubleshooting tips or even pointing out I'm being a muppet would be useful.

Cheers.

I can only assume the issue was something to do with the config classes in the site assembly.

Even after trying online examples, copypasting into the project, not even they worked.

As soon as I put the configuration section/element classes in a separate project (moved out of the website project) it started working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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