Fix web.config patch: use XPath instead of property navigation
Property-path navigation returns an empty string when the <location>
wrapper is absent from the published web.config, causing AppendChild
to fail. SelectSingleNode("//aspNetCore") works regardless of structure.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+12
-6
@@ -68,24 +68,30 @@ pipeline {
|
||||
powershell '''
|
||||
$path = "C:\\inetpub\\wwwroot\\web.config"
|
||||
$xml = [xml](Get-Content $path)
|
||||
$aspNetCore = $xml.configuration.location.'system.webServer'.aspNetCore
|
||||
|
||||
# Use XPath so the structure (with or without <location> wrapper) doesn't matter
|
||||
$aspNetCore = $xml.SelectSingleNode("//aspNetCore")
|
||||
if ($null -eq $aspNetCore) {
|
||||
Write-Error "Could not find aspNetCore element in web.config"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Ensure environmentVariables element exists
|
||||
if (-not $aspNetCore.environmentVariables) {
|
||||
$envVarsNode = $aspNetCore.SelectSingleNode("environmentVariables")
|
||||
if ($null -eq $envVarsNode) {
|
||||
$envVarsNode = $xml.CreateElement("environmentVariables")
|
||||
$aspNetCore.AppendChild($envVarsNode) | Out-Null
|
||||
}
|
||||
|
||||
# Remove existing ASPNETCORE_ENVIRONMENT entry if present
|
||||
$existing = $aspNetCore.environmentVariables.environmentVariable |
|
||||
Where-Object { $_.name -eq "ASPNETCORE_ENVIRONMENT" }
|
||||
if ($existing) { $aspNetCore.environmentVariables.RemoveChild($existing) | Out-Null }
|
||||
$existing = $envVarsNode.SelectSingleNode("environmentVariable[@name='ASPNETCORE_ENVIRONMENT']")
|
||||
if ($existing) { $envVarsNode.RemoveChild($existing) | Out-Null }
|
||||
|
||||
# Add fresh entry
|
||||
$envVar = $xml.CreateElement("environmentVariable")
|
||||
$envVar.SetAttribute("name", "ASPNETCORE_ENVIRONMENT")
|
||||
$envVar.SetAttribute("value", "Development")
|
||||
$aspNetCore.environmentVariables.AppendChild($envVar) | Out-Null
|
||||
$envVarsNode.AppendChild($envVar) | Out-Null
|
||||
|
||||
$xml.Save($path)
|
||||
Write-Host "web.config patched: ASPNETCORE_ENVIRONMENT=Development"
|
||||
|
||||
Reference in New Issue
Block a user