How to use applicationSettings in the new web.config configuration in VS2010?
Asked Answered
F

5

19

I'm used to use web deployment projects. Currently I am developing a new web application with VS2010 and want to try to get along with the new web.config principle and deployment issues.

How can I replace a simple setting like

<applicationSettings>
  <NAMESPACE>
   <setting name="Testenvironment" serializeAs="String">
    <value>True</value>
   </setting>
  </NAMESPACE>
</applicationSettings>

I want to have this setting to be set to True in Debug, and false in Release. How must the entries in the Web.Debug.config and Web.Release.Config look like?

And by the way: Is there some documentation about the new web.config issue? Can't seem to google for the correct keywords.

Firstborn answered 2/4, 2010 at 19:44 Comment(0)
I
14

You could also use this way; for the prod environment for example.

<applicationSettings xdt:Transform="Replace">
  <NAMESPACE> 
   <setting name="Testenvironment" serializeAs="String"> 
    <value>False</value> 
   </setting> 
  </NAMESPACE> 
</applicationSettings> 

Regards.

Immunotherapy answered 21/6, 2010 at 8:59 Comment(1)
Because I still had this to figure out: A transformation does just apply when publishing a website/application, not when building/compiling it.Nigel
A
45

The best way would be to do the following:

<applicationSettings> 
  <NAMESPACE> 
   <setting name="Testenvironment" serializeAs="String"  xdt:Transform="Replace" xdt:Locator="Match(name)"> 
    <value>True</value> 
   </setting> 
  </NAMESPACE> 
</applicationSettings> 

Rather than Zubeyir suggestion as this one will only replace the specifed setting rather than replacing the WHOLE of the applicationSettings section.

Arcature answered 3/8, 2010 at 8:7 Comment(1)
yep to be more specific Jonathan Stanton suggestion is the best answer because the answer Zubeyir suggested will replace all applicationSettings and when you update your debug version by adding new settings and you forgot to update the release version as well every things well be replaced with release verion of webconfigMata
I
14

You could also use this way; for the prod environment for example.

<applicationSettings xdt:Transform="Replace">
  <NAMESPACE> 
   <setting name="Testenvironment" serializeAs="String"> 
    <value>False</value> 
   </setting> 
  </NAMESPACE> 
</applicationSettings> 

Regards.

Immunotherapy answered 21/6, 2010 at 8:59 Comment(1)
Because I still had this to figure out: A transformation does just apply when publishing a website/application, not when building/compiling it.Nigel
E
3

You should copy this setting to both web config files - Web.Debug.config and Web.Release.config and put the transformation attributes xdt:Transform="SetAttributes" xdt:Locator="Match(name)".

You can see this video tutorial - http://chriskoenig.net/index.php/2010/04/08/how-do-i-web-config-transformations-in-vs2010/

Hope that helps.

Elimination answered 12/5, 2010 at 8:40 Comment(1)
Sorry this isn't the right answer. "SetAttributes" just sets the attributes. If you want to change the value tag you need "Replace"!Gottwald
G
1

Here is a link with lots of samples on this theme: http://msdn.microsoft.com/en-us/library/dd465326.aspx.

But there seems to be a problem especially with Web.config transformations and applicationSettings: All answers on this query using xdt-Transform=“Replace” have the problem that they introduce additional white space into the deployed Web.config because of XML formatting. This leads to faulty behavior if you consume the resulting settings. There seems to be no solution. Here is my unanswered question on this problem: VS 2010 configuration transformation produces unwanted white space during deployment.

Gottwald answered 13/8, 2010 at 7:30 Comment(0)
F
1

You might also have a look at How to use web.config transforms to replace appSettings and connectionStrings

Firstborn answered 1/1, 2011 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.