How do I change an web.config setting using transformation syntax?

蹲街弑〆低调 提交于 2019-12-03 11:02:23

问题


I have a value stored in my web.config file that I would like to change when the site is published. I want to change it from TEST to LIVE.

<appSettings>
    <add key="RequestMode" value="TEST" />
    // other keys here
</appSettings>

Is this possible using web.config transformation syntax? If so, how?

Thanks.


回答1:


Yes this is possible with transformation syntax. This transformation should do the trick:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="RequestMode" value="LIVE" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
  </appSettings>
</configuration>



回答2:


This is possible out of the box using Visual Studio 2010. The only caveat is this process is run from within Visual Studio when you use the Publish facilities within it. You won't get the ability to get this transformation from MSBuild (which hampers automated builds).

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

Source: Web.Config Transformations VS 2010

Also ensure your Web.Config.XXXX tranformation file matches your build definition. For instance, debug and release are supported by default so you would need to have a Web.Config.Release to adjust the Web.Config when publishing in release mode.




回答3:


I found the code below at http://mundrisoft.com/tech-bytes/web-config-transformation-for-project-deployment/ , which was working:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings>
    <add name="MyDB" 
      connectionString="ReleaseSQLServer" 
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  </connectionStrings>
</configuration



回答4:


You can also use the CTT tool for performing web transformation CTT Website You can then after publishing your files, before deployment using powershell can transform the web config (keeping a transform file) and then simply change the name during deployment



来源:https://stackoverflow.com/questions/15683854/how-do-i-change-an-web-config-setting-using-transformation-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!