From 7e690e4a8d93595f8bb28c6bf26b5c26cc90840f Mon Sep 17 00:00:00 2001 From: Shad Storhaug Date: Thu, 27 Feb 2014 13:09:21 +0700 Subject: [PATCH] Fixes #289, bug in MvcSiteMapProvider.Web install.ps1 where missing appSettings, system.web, system.web.webPages.razor, and system.webServer elements were not being added to the configuration element in the XML. Also fixed logic in install.ps1 and uninstall.ps1 so it will install or uninstall the MVC >= 4 sections without having to update the code for future versions of MVC. --- .../mvcsitemapprovider.web/tools/install.ps1 | 54 ++++++++++++------- .../tools/uninstall.ps1 | 10 ++-- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/nuget/mvcsitemapprovider.web/tools/install.ps1 b/nuget/mvcsitemapprovider.web/tools/install.ps1 index 530f9340..5d639da9 100644 --- a/nuget/mvcsitemapprovider.web/tools/install.ps1 +++ b/nuget/mvcsitemapprovider.web/tools/install.ps1 @@ -35,10 +35,17 @@ function Add-Or-Update-AppSettings() { $web_config_path = Get-Web-Config-Path $xml.Load($web_config_path) + $conf = $xml.SelectSingleNode("configuration") + if ($conf -eq $null) + { + $conf = $xml.CreateElement("configuration") + $xml.AppendChild($conf) + } + $appSettings = $xml.SelectSingleNode("configuration/appSettings") if ($appSettings -eq $null) { $appSettings = $xml.CreateElement("appSettings") - $xml.AppendChild($appSettings) + $conf.AppendChild($appSettings) } # add or update MvcSiteMapProvider_UseExternalDIContainer @@ -86,10 +93,17 @@ function Add-Pages-Namespaces() { $web_config_path = Get-Web-Config-Path $xml.Load($web_config_path) + $conf = $xml.SelectSingleNode("configuration") + if ($conf -eq $null) + { + $conf = $xml.CreateElement("configuration") + $xml.AppendChild($conf) + } + $system_web = $xml.SelectSingleNode("configuration/system.web") if ($system_web -eq $null) { $system_web = $xml.CreateElement("system.web") - $xml.AppendChild($system_web) + $conf.AppendChild($system_web) } $pages = $xml.SelectSingleNode("configuration/system.web/pages") @@ -140,10 +154,17 @@ function Add-Razor-Pages-Namespaces() { # load Web.config as XML $xml.Load($web_config_path) + $conf = $xml.SelectSingleNode("configuration") + if ($conf -eq $null) + { + $conf = $xml.CreateElement("configuration") + $xml.AppendChild($conf) + } + $system_web_webpages_razor = $xml.SelectSingleNode("configuration/system.web.webPages.razor") if ($system_web_webpages_razor -eq $null) { $system_web_webpages_razor = $xml.CreateElement("system.web.webPages.razor") - $xml.AppendChild($system_web_webpages_razor) + $conf.AppendChild($system_web_webpages_razor) } $pages = $xml.SelectSingleNode("configuration/system.web.webPages.razor/pages") @@ -216,11 +237,17 @@ function Add-MVC4-Config-Sections() { $web_config_path = Get-Web-Config-Path $xml.Load($web_config_path) - # select the node + $conf = $xml.SelectSingleNode("configuration") + if ($conf -eq $null) + { + $conf = $xml.CreateElement("configuration") + $xml.AppendChild($conf) + } + $ws = $xml.SelectSingleNode("configuration/system.webServer") if ($ws -eq $null) { $ws = $xml.CreateElement("system.webServer") - $xml.AppendChild($ws) + $conf.AppendChild($ws) } $modules = $xml.SelectSingleNode("configuration/system.webServer/modules") @@ -301,21 +328,12 @@ if ([string](InferPreferredViewEngine) -eq 'aspx') { (Get-Project).ProjectItems | ?{ $_.Name -eq "Views" } | %{ $_.ProjectItems | ?{ $_.Name -eq "Shared" } } | %{ $_.ProjectItems | ?{ $_.Name -eq "DisplayTemplates" } } | %{ $_.ProjectItems | ?{ $_.Name -eq "MenuHelperModel.ascx" -or $_.Name -eq "SiteMapHelperModel.ascx" -or $_.Name -eq "SiteMapNodeModel.ascx" -or $_.Name -eq "SiteMapNodeModelList.ascx" -or $_.Name -eq "SiteMapPathHelperModel.ascx" -or $_.Name -eq "SiteMapTitleHelperModel.ascx" -or $_.Name -eq "CanonicalHelperModel.ascx" -or $_.Name -eq "MetaRobotsHelperModel.ascx" } } | %{ $_.Delete() } } - -$mvc_version = $project.Object.References.Find("System.Web.Mvc").Version - -Write-Host "MVC Version: " -Write-Host $mvc_version - # If MVC 4 or higher, install web.config section to fix 404 not found on sitemap.xml (#124) -if ($project.Object.References.Find("System.Web.Mvc").Version -eq "4.0.0.0") -{ - Write-Host "Detected MVC 4" - Add-MVC4-Config-Sections -} -if ($project.Object.References.Find("System.Web.Mvc").Version -eq "5.0.0.0") +$mvc_version = $project.Object.References.Find("System.Web.Mvc").Version +Write-Host "MVC Version: $mvc_version" +if ($mvc_version -notmatch '^[123]\.' -or [string]::IsNullOrEmpty($mvc_version)) { - Write-Host "Detected MVC 5" + Write-Host "Installing config sections for MVC >= 4" Add-MVC4-Config-Sections } diff --git a/nuget/mvcsitemapprovider.web/tools/uninstall.ps1 b/nuget/mvcsitemapprovider.web/tools/uninstall.ps1 index b605a5a8..664ad8ca 100644 --- a/nuget/mvcsitemapprovider.web/tools/uninstall.ps1 +++ b/nuget/mvcsitemapprovider.web/tools/uninstall.ps1 @@ -26,7 +26,7 @@ function Remove-AppSettings() { } $appSettings = $xml.SelectSingleNode("configuration/appSettings") - if ($appSettings -eq $null) { + if ($appSettings -ne $null) { if (($appSettings.HasChildNodes -eq $false) -and ($appSettings.Attributes.Count -eq 0)) { $appSettings.ParentNode.RemoveChild($appSettings) } @@ -198,10 +198,12 @@ function Remove-MVC4-Config-Sections() { $xml.Save($localPath.Value) } -# If MVC 4, remove web.config section to fix 404 not found on sitemap.xml (#124) -if ($project.Object.References.Find("System.Web.Mvc").Version -eq "4.0.0.0") +# If MVC 4 or higher, remove web.config section to fix 404 not found on sitemap.xml (#124) +$mvc_version = $project.Object.References.Find("System.Web.Mvc").Version +Write-Host "MVC Version: $mvc_version" +if ($mvc_version -notmatch '^[123]\.' -or [string]::IsNullOrEmpty($mvc_version)) { - Write-Host "Detected MVC 4" + Write-Host "Removing config sections for MVC >= 4" Remove-MVC4-Config-Sections }