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>