chuckd
chuckd

Reputation: 14540

web.debug.config transform insert doesn't insert into web.config

Maybe I'm not using the transforms correctly, I'm just learning it, but when I fetch the key like below, running in debug mode, "stripeApiKey" is null. When I run in debug mode and hover over "ConfigurationManager.AppSettings" I can see all the keys that are cached in memory and I don't see it! Interesting! I see it in the transformed file but it isn't getting loaded into memory. Am I missing a step somewhere?

string stripeApiKey = ConfigurationManager.AppSettings["StripeApiKey"];

Here I'm trying to insert the "StripeApiKey" from the web.debug.config file

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
 <appSettings>
    <add key="StripeApiKey" value="sk_test_mytestkey123456" 
        xdt:Transform="Insert" 
        xdt:Locator="XPath(configuration/appSettings)" 
    />
 </appSettings>
</configuration>

Into my web.config file here

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings>
    </appSettings>
</configuration>

I also tried it with replace with no luck, so there must be something I'm missing in terms of location of hierarchy or something, how can I debug this?

Upvotes: 1

Views: 1079

Answers (1)

Tsahi Asher
Tsahi Asher

Reputation: 1810

XDT transforms are not executed during normal debug sessions. They only take place when you build the project for deployment. You should put your debug values in your Web.config file, and transform them to something else in Web.Release.config (or any other build configuration you have). The syntax would also be easier, using xdt:Locator="Match(key)", instead of using XPath.

Alternatively, you can use the SlowCheetah extension, which adds a build task that performs the transform. This will make the transformation in debug sessions too.

Upvotes: 2

Related Questions