Well I have following entries in my web config for SagePay Integration in an ASP.net MVC project.

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

However I am getting null when trying to access it from C# code.

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

Any help what I am missing here?

有帮助吗?

解决方案

You must cast the ConfigrationItem to "System.Configuration.IgnoreSectionHandler" or set the type Attribute in web.conf to an existing Type in your assembly.

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

After that you can access it

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

EDIT:

The Type must derive from ConfigurationSection.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top