ASP.NET MVC3: Debug and Release app settings not w

2019-02-02 03:35发布

问题:

This question already has an answer here:

  • How can I use Web.debug.config in the built-in visual studio debugger server? 7 answers

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?

回答1:

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.



回答2:

<!-- 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>


回答3:

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



回答4:

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)"/>


回答5:

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.



回答6:

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.