开发者

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜