2012-06-14 20 views
193

¿Es posible transformar el siguiente archivo Web.config appsettings:Cómo cambiar el valor del atributo en la sección appsettings con la transformación Web.config

<appSettings> 
    <add key="developmentModeUserId" value="00297022" /> 
    <add key="developmentMode" value="true" /> 
    /* other settings here that should stay */ 
</appSettings> 

en algo como esto:

<appSettings> 
    <add key="developmentMode" value="false" /> 
    /* other settings here that should stay */ 
</appSettings> 

Por lo tanto, necesito eliminar la clave developmentModeUserId, y debo reemplazar el valor de la clave developmentMode.

Respuesta

345

quieres algo así como:

<appSettings> 
    <add key="developmentModeUserId" xdt:Transform="Remove" xdt:Locator="Match(key)"/> 
    <add key="developmentMode" value="false" xdt:Transform="SetAttributes" 
      xdt:Locator="Match(key)"/> 
</appSettings> 

Ver http://msdn.microsoft.com/en-us/library/dd465326(VS.100).aspx para obtener más información.

+20

¡Tenga en cuenta que las teclas distinguen entre mayúsculas y minúsculas! – Cosmin

+1

Excelente respuesta. Estaba probando opciones de terceros como Slow Cheetah y llegar a ninguna parte, esto era simple y perfecto. – Steve

+2

@stevens: Necesitaría Slow Cheetah si quiere transformar, por ejemplo, archivos de aplicación.config para aplicaciones nativas. La sintaxis, sin embargo, debería ser idéntica si no recuerdo (ha pasado un tiempo desde que tuve que usar Slow Cheetah). – Ellesedil

0

Sustitución de todos los AppSettings

This is the overkill case where you just want to replace an entire section of the web.config. In this case I will replace all AppSettings in the web.config will new settings in web.release.config. This is my baseline web.config appSettings: 


<appSettings> 
    <add key="KeyA" value="ValA"/> 
    <add key="KeyB" value="ValB"/> 
</appSettings> 

Now in my web.release.config file, I am going to create a appSettings section except I will include the attribute xdt:Transform=”Replace” since I want to just replace the entire element. I did not have to use xdt:Locator because there is nothing to locate – I just want to wipe the slate clean and replace everything. 


<appSettings xdt:Transform="Replace"> 
    <add key="ProdKeyA" value="ProdValA"/> 
    <add key="ProdKeyB" value="ProdValB"/> 
    <add key="ProdKeyC" value="ProdValC"/> 
</appSettings> 



Note that in the web.release.config file my appSettings section has three keys instead of two, and the keys aren’t even the same. Now let’s look at the generated web.config file what happens when we publish: 


<appSettings> 
    <add key="ProdKeyA" value="ProdValA"/> 
    <add key="ProdKeyB" value="ProdValB"/> 
    <add key="ProdKeyC" value="ProdValC"/> 
</appSettings> 

tal y como esperábamos - los appsettings web.config fueron totalmente reemplazados por los valores de configuración web.release. ¡Eso fue fácil!

Cuestiones relacionadas