C# 如何使用 Web.config 转换更改 appSettings 部分中的属性值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11033001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:12:46  来源:igfitidea点击:

How to change the value of attribute in appSettings section with Web.config transformation

c#asp.net.netweb-config

提问by dragonfly

Is it possible to transform the following Web.config appSettings file:

是否可以转换以下 Web.config appSettings 文件:

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

into something like this:

变成这样:

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

So, I need to remove the key developmentModeUserId, and I need to replace the value for the key developmentMode.

因此,我需要删除键developmentModeUserId,并且需要替换键developmentMode的值。

采纳答案by Joe

You want something like:

你想要这样的东西:

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

See Also: Web.config Transformation Syntax for Web Application Project Deployment

另请参阅:Web 应用程序项目部署的 Web.config 转换语法

回答by Joe

Replacing all AppSettings

替换所有 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:

这是一种矫枉过正的情况,您只想替换 web.config 的整个部分。在这种情况下,我将替换 web.config 中的所有 AppSettings 并将 web.release.config 中的新设置。这是我的基线 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.

现在在我的 web.release.config 文件中,我将创建一个 appSettings 部分,除了我将包含属性 xdt:Transform=”Replace” 因为我只想替换整个元素。我不必使用 xdt:Locator 因为没有什么可定位的——我只是想把石板擦干净并更换所有东西。

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

请注意,在 web.release.config 文件中,我的 appSettings 部分有三个键而不是两个,而且这些键甚至都不相同。现在让我们看看生成的 web.config 文件,当我们发布时会发生什么:

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

Just as we expected – the web.config appSettings were completely replaced by the values in web.release config. That was easy!

正如我们预期的那样 - web.config appSettings 被 web.release 配置中的值完全取代。那很简单!

回答by Debendra Dash

If you want to make transformation your app setting from web config file to web.Release.config,you have to do the following steps. Let your web.config app setting file is this-

如果要将应用设置从 web 配置文件转换为 web.Release.config,则必须执行以下步骤。让您的 web.config 应用程序设置文件是这样的-

<appSettings>
     <add key ="K1" value="Debendra Dash"/>
  </appSettings>

Now here is the web.Release.config for the transformation.

现在这里是用于转换的 web.Release.config。

<appSettings>
    <add key="K1" value="value dynamicly from Realease"
       xdt:Transform="SetAttributes"
          xdt:Locator="Match(key)"   
         />
  </appSettings>

This will transform the value of K1 to the new value in realese Mode.

这将在 realese 模式下将 K1 的值转换为新值。

回答by CodingYoshi

I do not like transformations to have any more info than needed. So instead of restating the keys, I simply state the conditionand intention. It is much easier to see the intention when done like this, at least IMO. Also, I try and put all the xdtattributes first to indicate to the reader, these are transformations and not new things being defined.

我不喜欢转换有比需要更多的信息。因此,我没有重述关键,而是简单地陈述条件意图。这样做时更容易看出意图,至少在 IMO。此外,我尝试将所有xdt属性放在首位以向读者表明,这些是转换而不是正在定义的新事物。

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

In the above it is much easier to see that the first one is removing the element. The 2nd one is setting attributes. It will set/replace any attributes you define here. In this case it will simply set valueto false.

在上面很容易看到第一个是删除元素。第二个是设置属性。它将设置/替换您在此处定义的任何属性。在这种情况下,它将简单地设置valuefalse