簡體   English   中英

如何使用 XDT 在 web.config 中找到一個元素,然后將其更改為 nuget deploy

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM