![](/img/trans.png)
[英]How to upgrade (merge) web.config with web deploy (msdeploy)?
[英]How to use XDT to find an element in web.config then change it for nuget deploy
我需要使用 nuget 部署我的工作,并在此过程中更改 web.config。 我使用 XDT 添加以下代码:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="MyModule" type="My.Module" />
</modules>
</system.webServer>
我写了一个简单的 XDT web.config.install.xdt,它看起来像这样:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.webServer xdt:Transform="InsertIfMissing">
<modules runAllManagedModulesForAllRequests="true">
</modules>
</system.webServer>
<system.webServer>
<modules>
<add name="MyModule" type="My.Module" xdt:Transform="InsertIfMissing" />
</modules>
</system.webServer>
</configuration>
这很好用。 直到我遇到一个系统将他们的模块放在位置而不是配置下,就像这样:
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="MyModule" type="My.Module"/>
</modules>
....
所以在这种情况下,我的 XDT 找不到路径并在文件末尾创建一个新元素,这会终止该站点。 如何搜索文件中的任何位置是否存在 system.webServer 并在其中添加我的代码?
经过很长时间的搜索,我终于在网上找到了一些代码来解决这个问题。 我将其张贴在这里,以防有人会寻找类似的东西。 一、Kevin.Wu的原代码: http://git.crmclick.com:8888/kevin.wu/qa/blob/1c554bd0867de42ba360eb546d74e86ebf64af7b/packages/Microsoft.ApplicationInsights.887636.content.net.config/content.net.config/packages/Microsoft.ApplicationInsights/content.webconfig.net xdt
完成我需要的修改后的代码:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.webServer xdt:Transform="InsertIfMissing">
</system.webServer>
<system.webServer xdt:Locator="XPath(//system.webServer[(count(parent::location) = 0) or (count(parent::location[@path != '.' and count(@path) != 0]) = 0)])">
<validation validateIntegratedModeConfiguration="false" xdt:Transform="InsertIfMissing" />
</system.webServer>
<system.webServer xdt:Locator="XPath(//system.webServer[(count(parent::location) = 0) or (count(parent::location[@path != '.' and count(@path) != 0]) = 0)])">
<modules xdt:Transform="InsertIfMissing">
<add name="MyModule" type="My.Module" preCondition="managedHandler" xdt:Transform="InsertIfMissing" xdt:Locator="Match(type)"/>
</modules>
</system.webServer>
</configuration>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.