网络.配置自定义部分返回null

本文关键字:返回 null 自定义部 配置 网络 | 更新日期: 2023-09-27 17:51:08

我有以下条目在我的web配置SagePay集成在一个ASP.net MVC项目。

<configuration>
  <configSections>
<section name="SagePayConfiguration" type="System.Configuration.IgnoreSectionHandler" requirePermission="true" restartOnExternalChanges="true" allowLocation="true"/>
</configSections> 
<SagePayConfiguration>
    <!--Mandatory     
    Set to TEST for the Test Server and LIVE for the live environment-->
    <add key="sagepay.api.env" value="TEST" />
    .
    .
</SagePayConfiguration>

然而,当我试图从c#代码访问它时,我得到null。

SagePayConfiguration sagePayConfiguration = (SagePayConfiguration)System.Configuration.ConfigurationManager.GetSection("SagePayConfiguration");

我在这里错过了什么帮助吗?

网络.配置自定义部分返回null

您必须将ConfigrationItem强制转换为"System.Configuration "。或将web.conf中的type属性设置为程序集中现有的type。

<section name="<SectionName>" type="<your type (FQN)>, <Assembly>" />

之后你可以访问它

YourType o = ((YourType)ConfigurationManager.GetSection("sectionName"));

编辑:

Type必须来自ConfigurationSection