Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC3: Debug and Release app settings not working [duplicate]

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="[email protected]" />
    <add key="TestModeEmailAddress" value="[email protected]" />
</appSettings>

Web.Release.config

<appSettings>
    <add key="ErrorEmailAddress" value="[email protected]" />
</appSettings>

However, calling:

WebConfigurationManager.AppSettings["ErrorEmailAddress"]

is returning null (when debugging).

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

<add key="ErrorEmailAddress" value="[email protected]" xdt:Transform="Insert" />

Any ideas?

like image 877
Alistair Avatar asked Jun 02 '11 00:06

Alistair


4 Answers

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.

like image 144
Alistair Avatar answered Oct 27 '22 10:10

Alistair


<!-- 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>
like image 22
Bjoern Avatar answered Oct 27 '22 11:10

Bjoern


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

like image 34
Mustafa Magdy Avatar answered Oct 27 '22 09:10

Mustafa Magdy


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)"/>
like image 33
RPM1984 Avatar answered Oct 27 '22 10:10

RPM1984