繁体   English   中英

Web.Config自定义部分返回null

[英]Web.Config custom section returns null

好吧,我的Web配置中有以下条目用于ASP.net MVC项目中的SagePay集成。

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

有什么帮助我在这里想念的吗?

您必须将ConfigrationItem强制转换为“ System.Configuration.IgnoreSectionHandler”,或将web.conf中的Attribute类型设置为程序集中的现有Type。

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

之后,您可以访问它

YourType o =(((YourType)ConfigurationManager.GetSection(“ sectionName”)));

编辑:

类型必须派生自ConfigurationSection。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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