I have a web.config file that’s transformed using a web.debug.config file. Trying to get access to these values through System.Configuration.ConfigurationManager.AppSettings is yielding nothing.
My web.config appSettings are empty.
<configuration>
<appSettings>
</appSettings>
</configuration>
My web.debug.config transform that’s applied. Here’s a sample.
<configuration>
<appSettings>
<add key="CommonURL" value="localhost/mysite/" xdt:Transform="Insert" />
</appSettings>
</configuration>
Here’s the code to try and get this value, which returns null.
var cu = System.Configuration.ConfigurationManager.AppSettings["CommonURL"];
Any idea on why this could be?
Assuming that
System.Configuration.ConfigurationManager.AppSettingsis yielding nothing when you run from Visual Studio, then that is the expected behavior. You should add theCommonURLappSetting to yourweb.configfile and change the entry in theweb.debug.configto:These changes will allow you to get the value specified in the
web.configwhen you run from Visual Studio, and will allow that value to be replaced with what is defined in theweb.debug.configwhen you execute the transform (for example, through the Publish function or through a custom MSBuild script). Keep in mind that the transforms are applied when you publish, not when you start running a debug or release build. The values that are in the web.config file are always the ones that are honored.