29

My debug and release web.config app settings are not being read correctly.

Web.config:

<appSettings>
 <add key="webpages:Version" value="1.0.0.0"/>
 <add key="ClientValidationEnabled" value="true"/>
 <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>

Web.Debug.config

<appSettings>
    <add key="ErrorEmailAddress" value="developerEmail@email.com" />
    <add key="TestModeEmailAddress" value="developerEmail@email.com" />
</appSettings>

Web.Release.config

<appSettings>
    <add key="ErrorEmailAddress" value="prodErrorEmail@email.com" />
</appSettings>

However, calling:

WebConfigurationManager.AppSettings["ErrorEmailAddress"]

is returning null (when debugging).

I have tried adding xdt:Transform="Insert" e.g.

<add key="ErrorEmailAddress" value="prodErroEmail@email.com" xdt:Transform="Insert" />

Any ideas?

S..
  • 5,511
  • 2
  • 36
  • 43
Alistair
  • 1,939
  • 2
  • 22
  • 31

6 Answers6

52

Ok I figured it out.

Answered here: How can I use Web.debug.config in the built-in visual studio debugger server?

So the config files are only combined when you publish, not when you are running against a local server. Pretty stupid IMO, when else would you ever use Web.Debug.config?

I will do as is suggested here: Use Visual Studio web.config transform for debugging

and just have Web.config as my default debugging config file, then have release for when releasing. Can't see a use for Web.Debug.config as this point.

Still, this is annoying because most of my settings I want to be set one way for all environments but when developing (eg customErrors On). This means I have to set them in Web.config for debugging, then in all my other environment configs change them.

Thanks everyone for responses.

Community
  • 1
  • 1
Alistair
  • 1,939
  • 2
  • 22
  • 31
  • It seems one of the other answers on the second link might be more to your liking. You can use a debug post build step to run the transform. Interesting, the bug for it says its closed and fixed: https://connect.microsoft.com/VisualStudio/feedback/details/523221/have-web-debug-config-apply-during-development – m4tt1mus Jun 03 '11 at 18:20
  • yeah saw that, but too hacky for my likings – Alistair Jun 05 '11 at 01:55
10
<!-- Web.Config -->
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings configSource="AppSettings.config" />
</configuration>

<!-- AppSettings.config -->
<appSettings>
<add key="MyDoe" value="Arnold" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
</appSettings>

<!-- Web.Release.Config -->
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<compilation xdt:Transform="RemoveAttributes(debug)" />
<appSettings>
<add key="MyDoe" value="John" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
</appSettings>
</configuration>
Bjoern
  • 101
  • 1
  • 2
  • Thanks I was trying to figure out how to move section from my ASP.NET MVC Apps web.config and into an app.config. I assume I can do the same w/log4net settings, et al, with a line like this: ... will try! – Shawn J. Molloy Jun 22 '13 at 01:40
8

I found it out,

first you will add the app settings entries on appSetting of the web.Config with empty values or with debug values

<add key="Environment" value="Localhost" />

then you add the the same with the different values on the web.release.config but add the transformation part

 <add key="Environment" value="DifferentValue"  xdt:Transform="Replace" xdt:Locator="Match(key)"/>

Then when you publish the website on release mode you'll get the release values, You can also add the same to debug config then publish in debug config with different values

Mustafa Magdy
  • 1,230
  • 5
  • 28
  • 44
2

I've never had it working with not having the key in the default web.config.

This works for me:

Web.config

<add key="Environment" value="Localhost" />

Web.Debug.config

<add key="Environment" value="Development" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>

Web.Release.config

<add key="Environment" value="Production" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
RPM1984
  • 72,246
  • 58
  • 225
  • 350
  • Still doesn't work. Not sure what I'm doing wrong. – Alistair Jun 02 '11 at 03:18
  • 1
    @Alistair - have you tried with `ConfigurationManager` instead of `WebConfigurationManager`? I've never used `WebConfigurationManager`. – RPM1984 Jun 02 '11 at 03:30
  • 1
    @Alistair - okay, well im out of ideas. I was going to say check your build configuration for any post-build tasks, etc. But if this is just local debugging, not sure what else could be happening. – RPM1984 Jun 02 '11 at 03:45
1

Could you maybe post all of your web.configs? Default, debug, and release? One way to test if it's working is maybe set something like different connection strings for debug and release and check which one it uses when your app is running.

mymex1
  • 148
  • 3
0

are you debugging in release mode? in the toolbar next to the green arrow used to start debugging you can set a mode, make sure it isn't on release.

m4tt1mus
  • 1,642
  • 14
  • 24