1

I have a Web.config file with some entries in there but depending on whether I am in Debug or Release mode when I execute my Web Application locally, I want to take different appSettings.

For instance, let's say that I have the following entry in my Web.Debug.config appSettings.

<add key="MyServiceUrl" value="http://my-test-site:8080/" />

And also I have this in my Web.Release.config:

<add key="MyServiceUrl" value="http://my-prod-site:80/" />

How should I configure my Web.Config, Web.Debug.Config and Web.Release.Config so depending on the mode I run my application locally (Debug - Any CPU vs. Release - Any CPU), it takes the right one?

Right now, the only pair of key and value that it takes is the one from the main Web.Config regardless I select Debug or Release in Visual Studio.

How can I configure that behavior?

EDIT

This is how I have defined Web.config

<appSettings>
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

This is how I have defined Web.Debug.config

<appSettings>
     <add key="MyServiceUrl" value="http://my-test-site:8080/" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>

This is how I have defined Web.Release.config

<appSettings>
     <add key="MyServiceUrl" value="http://my-prod-site:8080/" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>

Finally, in my code, I have the following method:

public static string GetAppSetting(string settingKey)
        {
            if (string.IsNullOrEmpty(settingKey))
                throw new System.ArgumentException("Cannot fetch a setting for a null/empty setting key.");
            return ConfigurationManager.AppSettings[settingKey];
        }

which I call it like this: string setting = GetAppSetting("MyServiceUrl");

However, it is null if it is not defined in the main Web.config

2
  • Please add <add key="MyServiceUrl" to your web.config Commented Dec 1, 2016 at 22:54
  • Or use: xdt:Transform="Insert" Commented Dec 1, 2016 at 23:00

1 Answer 1

1

In the web.Release.config try this, it should work:

<appSettings>
 <add key="MyServiceUrl" value="http://my-prod-site:8080/" xdt:Transform="Insert" />
</appSettings>

Read this: Web.config Transformation Syntax for Web Application Project Deployment

Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately, exact same thing happens. None of the entries get populated when I call GetAppSetting if the entry does not exist in the web.config. And... if it exists it takes whatever it is in the main web.config and not from the Debug or Release ones..
What if you remove static? from: public static string GetAppSetting(string settingKey)
It is the same thing. It gets the one from the main web.config
It should work, ultimately you could share your solution. And I could load it.
Actually I was able to make it work with your suggestion and also this instructions: sebnilsson.com/blog/… Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.