简体   繁体   中英

How to use XDT to find an element in web.config then change it for nuget deploy

I need to deploy my work using nuget and to change the web.config in the process. I used XDT to add the following code:

    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
            <add name="MyModule" type="My.Module" />
        </modules>
    </system.webServer>

I wrote a simple XDT web.config.install.xdt which looks like this:

<?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>

And this works great. Until I met a system that puts their module under location instead of under configuration, like this:

<configuration>
  <location path="." inheritInChildApplications="false">
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
          <add name="MyModule" type="My.Module"/> 
        </modules>
....

So in this case, my XDT doesn't find the path and creates a new element towards the end of the file, which kills the site. How do I search for whether system.webServer exists anywhere in the file and add my code there?

After searching for a LONG time, I finally found some code online that resolved this for me. I am posting it here in case anyone will ever look for something similar. First, Kevin.Wu's original code: http://git.crmclick.com:8888/kevin.wu/qa/blob/1c554bd0867de42ba360eb546d74e86ebf64af7b/packages/Microsoft.ApplicationInsights.Web.2.0.0/content.net45/web.config.install.xdt

The modified code that does what I need:

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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