asp.net-mvc-4 - ASP.NET MVC 自定义配置 GetSection 返回 null
问题描述
我正在使用 .net 框架 4.5 处理继承的 ASP.NET MVC 4 项目。
我们添加了一个新的配置节文件和相关的类文件,据我们所知(docs.Microsoft 和其他在线指南),它设置正确。
问题
ConfigurationManager.GetSection()
返回空值。
根据文档,如果该部分不存在,则返回 null 。对此进行故障排除很麻烦。
编码
该网站是一个 ASP.NET Web 应用程序。属性窗口将程序集名称设置为 Client.Project.UI.Base(这是已发布 bin 中的 DLL)。这是用于配置类型 FQN 和 web.config 中的程序集的程序集名称。
注意:配置部分 SupportCaseConfiguration 最初位于一个单独的文件中,而 SupportTickets 部分只是指定了 configSource。这已移至 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 中看到配置部分。
我一直在看这个,我再也看不出有什么不对劲了。对我来说一切都很好...
任何想法、故障排除技巧甚至指出我是布偶都会很有用。
干杯。
解决方案
我只能假设问题与站点程序集中的配置类有关。
即使在尝试了在线示例,复制粘贴到项目中之后,它们甚至都不起作用。
一旦我将配置部分/元素类放在一个单独的项目中(从网站项目中移出),它就开始工作了。
推荐阅读
- javascript - 用于从数据表导出的角度 js 文件保护程序
- c++ - “catch” 无法从库中捕获异常
- c++ - 带有野兽增强的 OAuth2 返回临时重定向 307
- javascript - 多个元素上的航点 - 更新标题
- angular - 限制表格在角度 4 上的显示
- c# - 如何编写大型、可维护的编译时列表
> 元素可以相互调用, - python - pyCharm重构python类属性名,不重命名所有属性用法
- angularjs - 如何在我的 app.js 文件中引用 angular.min.js 文件?
- android - Ionic Cordova 通过从最近的应用程序列表中滑动应用程序被杀死时检测应用程序终止事件
- java - Keycloak 身份验证流程