Web.Config自定义部分返回null

时间:2014-03-21 06:56:50

标签: c# asp.net configuration web-config sagepay

我在ASP.net MVC项目中的SagePay Integration的Web配置中有以下条目。

<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");

我在这里缺少什么帮助?

1 个答案:

答案 0 :(得分:1)

您必须将ConfigrationItem强制转换为&#34; System.Configuration.IgnoreSectionHandler&#34;或者将web.conf中的类型Attribute设置为程序集中的现有Type。

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

之后你可以访问它

YourType o =((YourType)ConfigurationManager.GetSection(&#34; sectionName&#34;));

编辑:

Type必须来自ConfigurationSection。

相关问题