Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 有没有办法做一个;替换或插入“;使用web.config转换?_Asp.net_Web Config Transform_Xslt_Xdt Transform - Fatal编程技术网

Asp.net 有没有办法做一个;替换或插入“;使用web.config转换?

Asp.net 有没有办法做一个;替换或插入“;使用web.config转换?,asp.net,web-config-transform,xslt,xdt-transform,Asp.net,Web Config Transform,Xslt,Xdt Transform,我正在使用下面文章中描述的web.config转换,以便为不同的环境生成配置 我可以通过匹配键来进行“替换”转换,例如 <add key="Environment" value="Live" xdt:Transform="Replace" xdt:Locator="Match(key)" /> 我可以做“插入”,例如 但是我发现真正有用的是ReplaceOrInsert转换,因为我不能总是依赖于原始配置文件有/没有特定的密钥 有什么办法可以做到这一点吗?我找到了一个便宜的

我正在使用下面文章中描述的web.config转换,以便为不同的环境生成配置

我可以通过匹配键来进行“替换”转换,例如

<add key="Environment" value="Live" xdt:Transform="Replace" xdt:Locator="Match(key)" />

我可以做“插入”,例如


但是我发现真正有用的是ReplaceOrInsert转换,因为我不能总是依赖于原始配置文件有/没有特定的密钥


有什么办法可以做到这一点吗?

我找到了一个便宜的解决办法。如果有很多元素需要“替换或插入”,那么它就不好看,也不会很好地工作

先“删除”,然后“插入之后|插入之前”

比如说,

<authorization xdt:Transform="Remove" />
<authorization xdt:Transform="InsertAfter(/configuration/system.web/authentication)">
  <deny users="?"/>
  <allow users="*"/>
</authorization>

对我来说,更好的方法是仅在元素不存在时插入元素,因为我只设置了某些属性。删除该元素将丢弃主元素的任何其他属性(如果存在)

例如: web.config(不含元素)


web.config(带元素)


使用带有XPath表达式的定位器,如果节点不存在,则添加该节点,然后设置我的属性:

<serviceDebug xdt:Transform="Insert"
  xdt:Locator="XPath(/configuration/system.serviceModel/behaviors/serviceBehaviors/behavior[not(serviceDebug)])" />
<serviceDebug includeExceptionDetailInFaults="true" xdt:Transform="SetAttributes" />


生成的两个web.config文件都具有includeExceptionDetailInFaults=“true”,第二个文件保留了httpsHelpPageEnabled属性,而remove/insert方法不会保留该属性。

xdt:Transform=“remove”
结合使用VS2012中的
xdt:Transform=“InsertIfMissing”

<authorization xdt:Transform="Remove" />
<authorization xdt:Transform="InsertIfMissing">
  <deny users="?"/>
  <allow users="*"/>
</authorization>

使用
InsertIfMissing
转换确保appSetting存在。
然后使用
Replace
转换设置其值

<appSettings>
  <add key="Environment" xdt:Transform="InsertIfMissing" xdt:Locator="Match(key)" />
  <add key="Environment" value="Live" xdt:Transform="Replace" xdt:Locator="Match(key)" />
</appSettings>
这些技术比remove+insert好得多,因为现有节点不会移动到其父节点的底部。新节点附加在末尾。现有节点保留在源文件中的位置


此答案仅适用于较新版本的Visual Studio(2012或更高版本)。

我喜欢此想法,但如果元素已经存在,则会出现错误“源文档中没有匹配的元素…”。也就是说,如果它存在,“not”将失败,因此这是一个错误。这是您在使用不支持新(ish)“InsertIfMissing”元素的XDT版本时所需要的技术。完美!这就是我们一直在等待的。这根本不符合OP的要求。答案经过编辑,以更清楚地说明它如何回答原始问题。我不明白。如果你删除它,它当然会丢失,在那一点上它只是一个插入,对吗?@ChadSchouggins不一定:
remove
任务只删除第一次出现的内容。某些图元可以多次出现。我无法想象您会想要这样做,但它会删除第一个匹配项,并跳过
InsertIfMissing
任务。但是如果他使用
RemoveAll
来代替,你可能是对的。如果使用VS2012,现在有了更好的解决方案。请参见下文“InsertIfMissing”是否会在需要时插入和替换?由于使用InsertAfter,我更喜欢此选项而不是其他选项。如果您仍在执行删除操作,则插入缺少将毫无意义。您提供的链接目前不起作用。你有其他链接可以轻松理解这个概念吗?@AshishJain这个链接对我来说很好
<serviceDebug xdt:Transform="Insert"
  xdt:Locator="XPath(/configuration/system.serviceModel/behaviors/serviceBehaviors/behavior[not(serviceDebug)])" />
<serviceDebug includeExceptionDetailInFaults="true" xdt:Transform="SetAttributes" />
<authorization xdt:Transform="Remove" />
<authorization xdt:Transform="InsertIfMissing">
  <deny users="?"/>
  <allow users="*"/>
</authorization>
<appSettings>
  <add key="Environment" xdt:Transform="InsertIfMissing" xdt:Locator="Match(key)" />
  <add key="Environment" value="Live" xdt:Transform="Replace" xdt:Locator="Match(key)" />
</appSettings>
<appSettings>  
  <add key="UseLivePaymentService" xdt:Transform="InsertIfMissing" xdt:Locator="Match(key)" />
  <add key="UseLivePaymentService" value="true" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>