簡體   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