简体   繁体   English

如何转换web.config值?

[英]How can I transform web.config values?

I am trying to make different config files for different cases (debug,release, etc.) and I would like to change some settings for different builds. 我试图为不同的情况(调试,发布等)制作不同的配置文件,我想更改不同版本的一些设置。

<configuration>
<applicationSettings>
<Program1.Properties.Settings>
  <setting name="CustomerId" serializeAs="String">
    <value>Custormer1-13256</value>
  </setting>
</Program1.Properties.Settings>
</applicationSettings>
</configuration>

How can I change the value inside the tag to something else? 如何将标签内的值更改为其他值?

   ex. <value>Customer2-343242</value>

Add xdt:Locator="Match(name)" to Erwin's answer xdt:Locator="Match(name)"Erwin的答案中

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<applicationSettings>
    <Program1.Properties.Settings>
        <setting name="CustomerId" serializeAs="String" xdt:Transform="Replace" 
                                                             xdt:Locator="Match(name)">
            <value>Customer2-343242</value>
        </setting>
    </Program1.Properties.Settings>
</applicationSettings>

Change your config to look like this: 将配置更改为如下所示:

<configuration>
  <applicationSettings>
    <Program1.Properties.Settings>
      <setting name="CustomerId" serializeAs="String">
        <value>Custormer1-13256</value>
      </setting>
    </Program1.Properties.Settings>

  </applicationSettings>
</configuration>

And make a transform like this: 并进行如下转换:

<configuration>
  <applicationSettings>
    <Program1.Properties.Settings xdt:Transform="Replace">
      <setting name="CustomerId" serializeAs="String">
        <value>Custormer1-13256</value>
      </setting>
    </Program1.Properties.Settings>
  </applicationSettings>
</configuration>

So you aren't really "changing" the value inner text so much as having it choose the right value for each build configuration you create. 所以你并没有真正“改变”内部文本的值,而是让它为你创建的每个构建配置选择正确的值。

This works: 这有效:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <applicationSettings>
        <Program1.Properties.Settings>
            <setting name="CustomerId" serializeAs="String" xdt:Locator="Match(name)" >
                <value xdt:Transform="Replace">Customer2-343242</value>
            </setting>
        </Program1.Properties.Settings>
    </applicationSettings>
</configuration>

Use Web config transformations : 使用Web配置转换

<setting name="CustomerId" serializeAs="String" xdt:Transform="Replace">
  <value>Customer2-343242</value>
</setting>

You need to put the transform in the full path for your Web.Config XML 您需要将转换放在Web.Config XML的完整路径中

<applicationSettings>
  <Program1.Properties.Settings>
    <setting name="CustomerId">
      <value xdt:Transform="Replace">Customer2-343242</value>
    </setting>
  </Program1.Properties.Settings>
</applicationSettings>

Then test it using the Preview Transform menu option 然后使用“预览变换”菜单选项对其进行测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM