From 186c26f6a1bda9d20ff406299a3db5658796e116 Mon Sep 17 00:00:00 2001 From: moozzyk Date: Wed, 27 Jan 2016 16:51:01 -0800 Subject: [PATCH] Adding ASPNET_APPLICATIONBASE environment variable to fix MVC views in IIS --- src/dotnet-publish-iis/WebConfigTransform.cs | 19 ++++++ src/dotnet-publish-iis/project.json | 3 +- .../WebConfigTransformFacts.cs | 66 ++++++++++++------- 3 files changed, 62 insertions(+), 26 deletions(-) diff --git a/src/dotnet-publish-iis/WebConfigTransform.cs b/src/dotnet-publish-iis/WebConfigTransform.cs index 75e3cc8714..7a0a209cbd 100644 --- a/src/dotnet-publish-iis/WebConfigTransform.cs +++ b/src/dotnet-publish-iis/WebConfigTransform.cs @@ -64,6 +64,25 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS SetAttributeValueIfEmpty(httpPlatformElement, "stdoutLogEnabled", "false"); SetAttributeValueIfEmpty(httpPlatformElement, "stdoutLogFile", logPath); SetAttributeValueIfEmpty(httpPlatformElement, "startupTimeLimit", "3600"); + + AddApplicationBase(httpPlatformElement); + } + + private static void AddApplicationBase(XElement httpPlatformElement) + { + const string appBaseKeyName = "ASPNET_APPLICATIONBASE"; + + var envVariables = GetOrCreateChild(httpPlatformElement, "environmentVariables"); + var appBaseElement = envVariables.Elements("environmentVariable").SingleOrDefault(e => + string.Equals((string)e.Attribute("name"), appBaseKeyName, StringComparison.CurrentCultureIgnoreCase)); + + if (appBaseElement == null) + { + appBaseElement = new XElement("environmentVariable", new XAttribute("name", appBaseKeyName)); + envVariables.AddFirst(appBaseElement); + } + + appBaseElement.SetAttributeValue("value", "."); } private static XElement GetOrCreateChild(XElement parent, string childName) diff --git a/src/dotnet-publish-iis/project.json b/src/dotnet-publish-iis/project.json index eefde53ba1..cc40873f6a 100644 --- a/src/dotnet-publish-iis/project.json +++ b/src/dotnet-publish-iis/project.json @@ -6,7 +6,7 @@ }, "dependencies": { - "System.Xml.XDocument": "4.0.11-*", + "NETStandard.Library": "1.0.0-*", "Microsoft.Extensions.CommandLineUtils": "1.0.0-*", "Microsoft.Extensions.Configuration.Json": "1.0.0-*", "Microsoft.DotNet.ProjectModel": "1.0.0-*" @@ -14,7 +14,6 @@ "frameworks": { "dnxcore50": { - "Microsoft.NETCore.Platforms": "1.0.0-*" } } } diff --git a/test/dotnet-publish-iis.Tests/WebConfigTransformFacts.cs b/test/dotnet-publish-iis.Tests/WebConfigTransformFacts.cs index 753c05ad4e..e305c7e576 100644 --- a/test/dotnet-publish-iis.Tests/WebConfigTransformFacts.cs +++ b/test/dotnet-publish-iis.Tests/WebConfigTransformFacts.cs @@ -6,13 +6,17 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests { public class WebConfigTransformFacts { - private XDocument WebConfigTemplate => XDocument.Parse( + private XDocument WebConfigTemplate => XDocument.Parse( @" - + + + + + "); @@ -31,14 +35,16 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests } [Theory] - [InlineData(new object[] { new[] {"system.webServer"}})] - [InlineData(new object[] { new[] {"add"}})] - [InlineData(new object[] { new[] {"handlers"}})] - [InlineData(new object[] { new[] {"httpPlatform"}})] - [InlineData(new object[] { new[] {"handlers", "httpPlatform"}})] + [InlineData(new object[] { new[] { "system.webServer" } })] + [InlineData(new object[] { new[] { "add" } })] + [InlineData(new object[] { new[] { "handlers" } })] + [InlineData(new object[] { new[] { "httpPlatform" } })] + [InlineData(new object[] { new[] { "environmentVariables" } })] + [InlineData(new object[] { new[] { "environmentVariable" } })] + [InlineData(new object[] { new[] { "handlers", "httpPlatform", "environmentVariables" } })] public void WebConfigTransform_adds_missing_elements(string[] elementNames) { - var input = new XDocument(WebConfigTemplate); + var input = WebConfigTemplate; foreach (var elementName in elementNames) { input.Descendants(elementName).Remove(); @@ -50,7 +56,7 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests [Theory] [InlineData("add", "path", "test")] - [InlineData("add", "verb","test")] + [InlineData("add", "verb", "test")] [InlineData("add", "modules", "mods")] [InlineData("add", "resourceType", "Either")] [InlineData("httpPlatform", "stdoutLogEnabled", "true")] @@ -59,7 +65,7 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests [InlineData("httpPlatform", "stdoutLogFile", "logfile.log")] public void WebConfigTransform_wont_override_custom_values(string elementName, string attributeName, string attributeValue) { - var input = new XDocument(WebConfigTemplate); + var input = WebConfigTemplate; input.Descendants(elementName).Single().SetAttributeValue(attributeName, attributeValue); var output = WebConfigTransform.Transform(input, "test.exe", configureForAzure: false); @@ -79,7 +85,7 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests [Fact] public void WebConfigTransform_fixes_httpPlatformHandler_casing() { - var input = new XDocument(WebConfigTemplate); + var input = WebConfigTemplate; input.Descendants("add").Single().SetAttributeValue("name", "httpplatformhandler"); Assert.True(XNode.DeepEquals(WebConfigTemplate, @@ -89,22 +95,21 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests [Fact] public void WebConfigTransform_does_not_remove_children_of_httpPlatform_element() { - var envVarsElement = - new XElement("environmentVariables", - new XElement("environmentVariable", new XAttribute("name", "ENVVAR"), new XAttribute("value", "123"))); + var envVarElement = + new XElement("environmentVariable", new XAttribute("name", "ENVVAR"), new XAttribute("value", "123")); - var input = new XDocument(WebConfigTemplate); - input.Descendants("httpPlatform").Single().Add(envVarsElement); + var input = WebConfigTemplate; + input.Descendants("environmentVariable").Single().Add(envVarElement); - Assert.True(XNode.DeepEquals(envVarsElement, + Assert.True(XNode.DeepEquals(envVarElement, WebConfigTransform.Transform(input, "app.exe", configureForAzure: false) - .Descendants("httpPlatform").Elements().Single())); + .Descendants("environmentVariable").SingleOrDefault(e => (string)e.Attribute("name") == "ENVVAR"))); } [Fact] public void WebConfigTransform_adds_stdoutLogEnabled_if_attribute_is_missing() { - var input = new XDocument(WebConfigTemplate); + var input = WebConfigTemplate; input.Descendants("httpPlatform").Attributes("stdoutLogEnabled").Remove(); Assert.Equal( @@ -119,7 +124,7 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests [InlineData("true")] public void WebConfigTransform_adds_stdoutLogFile_if_attribute_is_missing(string stdoutLogFile) { - var input = new XDocument(WebConfigTemplate); + var input = WebConfigTemplate; var httpPlatformElement = input.Descendants("httpPlatform").Single(); httpPlatformElement.Attribute("stdoutLogEnabled").Remove(); @@ -140,7 +145,7 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests [InlineData("false")] public void WebConfigTransform_does_not_change_existing_stdoutLogEnabled(string stdoutLogEnabledValue) { - var input = new XDocument(WebConfigTemplate); + var input = WebConfigTemplate; var httpPlatformElement = input.Descendants("httpPlatform").Single(); httpPlatformElement.SetAttributeValue("stdoutLogFile", "mylog.txt"); @@ -159,18 +164,31 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests [Fact] public void WebConfigTransform_correctly_configures_for_Azure() { - var input = new XDocument(WebConfigTemplate); + var input = WebConfigTemplate; input.Descendants("httpPlatform").Attributes().Remove(); + var httPlatformElement = WebConfigTransform.Transform(input, "test.exe", configureForAzure: true) + .Descendants("httpPlatform").Single(); + httPlatformElement.Elements().Remove(); + Assert.True(XNode.DeepEquals( XDocument.Parse(@"").Root, - WebConfigTransform.Transform(input, "test.exe", configureForAzure: true).Descendants("httpPlatform").Single())); + httPlatformElement)); + } + + public void WebConfigTransform_overrites_value_for_ASPNET_APPLICATIONBASE() + { + var input = WebConfigTemplate; + input.Descendants("environmentVariable").Single().SetAttributeValue("value", "abc"); + + Assert.True(XNode.DeepEquals(WebConfigTemplate, + WebConfigTransform.Transform(input, "test.exe", configureForAzure: false))); } private bool VerifyMissingElementCreated(params string[] elementNames) { - var input = new XDocument(WebConfigTemplate); + var input = WebConfigTemplate; foreach (var elementName in elementNames) { input.Descendants(elementName).Remove();