I'm trying to follow the guidance here for reading data from a web.config file.
I have the following in my web.config file:
<configuration>
<appSettings>
<add key="customsetting1" value="customValue"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.data>
...
And to test the accessing of data I did the following:
@using System.Web
@using System.Configuration
@using System.Web.Configuration
@{
System.Configuration.Configuration rootWebConfig1 =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
string value = (string)
rootWebConfig1.AppSettings.Settings["customsetting1"].Value;
This led to the following error: System.NullReferenceException: Object reference not set to an instance of an object.
I have checked and found rootWebConfig1.AppSettings.Settings is not null, but rootWebConfig1.AppSettings.Settings["customsetting1"] is null.
What am I doing wrong?