简体   繁体   English

web.config 转换为<applicationSettings>

[英]web.config transforms for <applicationSettings>

I have web.config transforms for several environments.我有多种环境的 web.config 转换。 In the config file I have an applicationSettings section with several setting and value pairs.在配置文件中,我有一个包含多个设置和值对的 applicationSettings 部分。

I have tried based on the syntax I use to match name and change the connection strings to also match settings and change the value but the transforms are failing.我已经根据我用来匹配名称的语法进行了尝试,并将连接字符串更改为匹配设置并更改值,但转换失败。 Is this at all possible?这是可能吗?

So my web.config has:所以我的 web.config 有:

<applicationSettings>
    <AppName.My.MySettings>
        <setting name="setting1" serializeAs="String">
            <value>Initial Value</value>
        </setting>
    </AppName.My.MySettings>
</applicationSettings>

My transform file has我的转换文件有

<applicationSettings>
    <add name="setting1" value="Changed Value" xdt:Transform="SetAttributes" xdt:Location="Match(name)"/>
</applicationSettings>

I get no errors when I preview the transform but whereas the connection string setting are transformed the value for setting1 is not.预览转换时我没有收到任何错误,但连接字符串设置已转换,而 setting1 的值却没有。 Any help appreciated.任何帮助表示赞赏。

UPDATE更新

<applicationSettings>
    <add name="setting1" value="Changed Value" xdt:Transform="Replace" xdt:Location="Match(name)"/>
</applicationSettings>

Unfortunately same problem... No errors and no transform.不幸的是同样的问题......没有错误也没有转换。

SOLUTION I did forget to mention I have more than one setting so marked answer is partial solution... This is how I did it... Web.Config...解决方案我确实忘记提到我有不止一个设置,所以标记的答案是部分解决方案......我就是这样做的...... Web.Config ...

<applicationSettings>
    <AppName.My.MySettings>
        <setting name="setting1" serializeAs="String">
            <value>Initial Value 1</value>
        </setting>
        <setting name="setting2" serializeAs="String">
            <value>Initial Value 2</value>
        </setting>
        <setting name="setting3" serializeAs="String">
            <value>Initial Value 3</value>
        </setting>
    </AppName.My.MySettings>
</applicationSettings>

Transform File转换文件

<applicationSettings xdt:Transform="Replace">
    <AppName.My.MySettings>
        <setting name="setting1" serializeAs="String">
            <value>CHANGED VALUE 1</value>
        </setting>
        <setting name="setting2" serializeAs="String">
            <value>Initial value 2</value>
        </setting>
        <setting name="setting3" serializeAs="String">
            <value>CHANGED VALUE 3</value>
        </setting>
    </AppName.My.MySettings>
</applicationSettings>

Note I had to include all my nested settings and values even if some of them didn't change as in the case of setting 2 in my example.注意我必须包含所有嵌套的设置和值,即使其中一些没有像我的示例中的设置 2 那样更改。

I know this is a little late, but the following transform file will allow you transform just one setting when you have multiple.我知道这有点晚了,但是当您有多个设置时,以下转换文件将允许您只转换一个设置。

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <applicationSettings>
    <YourProject.Settings>
      <setting name="Log4NetPath" serializeAs="String" xdt:Transform="Replace" xdt:Locator="Match(name)">
        <value xdt:Transform="Replace">NewPath</value>
      </setting>
    </YourProject.Settings>
  </applicationSettings>
</configuration>

Transform file:转换文件:

<applicationSettings>
   <AppName.My.MySettings>
      <setting xdt:Transform="Replace" xdt:Locator="Match(name)" name="setting1" serializeAs="String">
           <value>New Value</value>
       </setting>
    </AppName.My.MySettings>
  </applicationSettings>

https://hk.saowen.com/a/7857cf5ec8f14af8504e7b9bb029e4cb0047336394e464a9b3807a9e1e587b93 https://hk.saowen.com/a/7857cf5ec8f14af8504e7b9bb029e4cb0047336394e464a9b3807a9e1e587b93

Using Transform and Locator Attributes on Separate Elements在单独的元素上使用变换和定位器属性

A Transform attribute does not have to be set in the same element as a Locator element.不必在与 Locator 元素相同的元素中设置 Transform 属性。 You can specify a Locator element on a parent element in order to select elements whose child elements you want to work with.您可以在父元素上指定一个 Locator 元素,以便选择要使用其子元素的元素。 You can then specify a Transform attribute in a child element in order to apply a change to the children.然后,您可以在子元素中指定 Transform 属性,以便将更改应用于子元素。

The following example shows how to use the Locator attribute to select location elements for the specified path.以下示例显示如何使用 Locator 属性为指定路径选择位置元素。 However, only elements that are children of the selected location elements are transformed.但是,仅变换所选位置元素的子元素。

<configuration xmlns:xdt="...">
  <location path="C:\MySite\Admin" xdt:Locator="Match(path)"> 
    <system.web>
      <pages viewStateEncryptionMode="Always"
        xdt:Transform="SetAttributes(viewStateEncryptionMode)" />
    </system.web> 
  </location> 
</configuration>

If you specify a Locator attribute but you do not specify a Transform attribute in the same element or in a child element, no changes are made.如果指定了 Locator 属性,但未在同一元素或子元素中指定 Transform 属性,则不会进行任何更改。

Agree with above answers.同意楼上的回答。 You need to determine if you replacing(transforming) a node or an attribute .您需要确定是替换(转换)节点还是属性

A node is value here: <value>Datasource=connection info</value>一个节点在这里是<value>Datasource=connection info</value>

An attribute is name here: <value name="connection info"/>一个属性是这里的名称<value name="connection info"/>

To replace a node , you use:要替换节点,请使用:

<value xdt:Transform="Replace">
Datasource="connection info";
</value>    

To replace an attribute :替换属性

<value name="other connection info"
   xdt:Transform="SetAttributes"
   xdt:Locator="Match(name)"/>

More detailed reference for replacing attributes: https://docs.microsoft.com/en-us/previous-versions/dd465326(v=vs.100)?redirectedfrom=MSDN更详细的替换属性参考: https : //docs.microsoft.com/en-us/previous-versions/dd465326(v=vs.100)?redirectedfrom=MSDN

original file:原始文件:

<applicationSettings>
    <AppName.My.MySettings>
        <setting name="setting1" serializeAs="String">
            <value>Initial Value</value>
        </setting>
    </AppName.My.MySettings>
</applicationSettings>

Transform file:转换文件:

 <applicationSettings>
   <AppName.My.MySettings>
      <setting name="setting1" serializeAs="String">
           <value xdt:Transform="Replace">Changed Value</value>
       </setting>
    </AppName.My.MySettings>
  </applicationSettings>

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

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