Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

web.config transform elements not available through System.Configuration.ConfigurationManager

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?

like image 954
Razor Avatar asked Oct 11 '22 14:10

Razor


1 Answers

Assuming that System.Configuration.ConfigurationManager.AppSettings is yielding nothing when you run from Visual Studio, then that is the expected behavior. You should add the CommonURL appSetting to your web.config file and change the entry in the web.debug.config to:

<add key="CommonURL" value="localhost/mysite/" 
    xdt:Transform="Replace" xdt:Locator="Match(key)" />

These changes will allow you to get the value specified in the web.config when you run from Visual Studio, and will allow that value to be replaced with what is defined in the web.debug.config when 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.

like image 69
rsbarro Avatar answered Oct 16 '22 04:10

rsbarro