Matthew Flynn
Matthew Flynn

Reputation: 3941

Modifying web.config with powershell in Asp.Net Core 2

I need to add the following to my published web.config;

<modules runAllManagedModulesForAllRequests="false">
    <remove name="WebDAVModule" />
</modules>

that I am publishing with Asp.Net Core 2, however, it doesnt seem to be working;

$webConfig = (Get-Item -Path ".\").FullName + "\Bemfeito.Services.WebApi\obj\Release\netcoreapp2.0\PubTmp\Out\web.config"
$doc = (gc $webConfig) -as [Xml]

$moduleNode = $doc.CreateElement("modules")
$moduleNode.SetAttribute("runAllManagedModulesForAllRequests", "false")
$removeNode = $moduleNode.CreateElement("remove")
$removeNode.SetAttribute("name","WebDAVModule")
$moduleNode.AppendChild($removeNode)

$doc.configuration.system.webServer.AppendChild($moduleNode)

$doc.Save($webConfig)

(I am adding this as a prepublish option on my webdeploy) I think it has something to do with not grabbing and/or commiting the saves correctly? can someone please advise me on where I am going wrong?

Upvotes: 1

Views: 973

Answers (1)

Matthew Flynn
Matthew Flynn

Reputation: 3941

There was a number of issues with appending to the node but the main one turned out to be the fact I was appending to configuration.system.webServer.AppendChild. In fact this should have been configuartion.'system.webServer'.AppendChild

For reference my full script is;

$webConfig = (Get-Item -Path ".\").FullName + 

"\Services.WebApi\obj\Release\netcoreapp2.0\PubTmp\Out\web.config"
$doc = (gc $webConfig) -as [Xml]

$moduleNode = $doc.CreateElement("modules")
$moduleNode.SetAttribute("runAllManagedModulesForAllRequests", "false")


$removeNode = $doc.CreateElement("remove")
$removeNode.SetAttribute("name","WebDAVModule")
$moduleNode.AppendChild($removeNode)

$doc.configuration.'system.webServer'.AppendChild($moduleNode)

$doc.Save($webConfig)

Upvotes: 1

Related Questions