【问题标题】:How to use XDT to find an element in web.config then change it for nuget deploy如何使用 XDT 在 web.config 中查找元素,然后将其更改为 nuget deploy
【发布时间】:2020-12-08 13:31:52
【问题描述】:

我需要使用 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 是否存在于文件中的任何位置并在那里添加我的代码?

【问题讨论】:

    标签: asp.net nuget web.config-transform xdt


    【解决方案1】:

    找了好久,终于在网上找到了一些代码可以帮我解决这个问题。 我把它贴在这里,以防有人会寻找类似的东西。 一、Kevin.Wu的原码:http://git.crmclick.com:8888/kevin.wu/qa/blob/1c554bd0867de42ba360eb546d74e86ebf64af7b/packages/Microsoft.ApplicationInsights.Web.2.0.0/content/net45/web.config.install.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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-11
      • 2023-03-31
      • 1970-01-01
      • 2014-10-08
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多