Adding ASPNET_APPLICATIONBASE environment variable to fix MVC views in IIS

This commit is contained in:
moozzyk 2016-01-27 16:51:01 -08:00
parent d264a86c5d
commit 186c26f6a1
3 changed files with 62 additions and 26 deletions

View File

@ -64,6 +64,25 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS
SetAttributeValueIfEmpty(httpPlatformElement, "stdoutLogEnabled", "false"); SetAttributeValueIfEmpty(httpPlatformElement, "stdoutLogEnabled", "false");
SetAttributeValueIfEmpty(httpPlatformElement, "stdoutLogFile", logPath); SetAttributeValueIfEmpty(httpPlatformElement, "stdoutLogFile", logPath);
SetAttributeValueIfEmpty(httpPlatformElement, "startupTimeLimit", "3600"); 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) private static XElement GetOrCreateChild(XElement parent, string childName)

View File

@ -6,7 +6,7 @@
}, },
"dependencies": { "dependencies": {
"System.Xml.XDocument": "4.0.11-*", "NETStandard.Library": "1.0.0-*",
"Microsoft.Extensions.CommandLineUtils": "1.0.0-*", "Microsoft.Extensions.CommandLineUtils": "1.0.0-*",
"Microsoft.Extensions.Configuration.Json": "1.0.0-*", "Microsoft.Extensions.Configuration.Json": "1.0.0-*",
"Microsoft.DotNet.ProjectModel": "1.0.0-*" "Microsoft.DotNet.ProjectModel": "1.0.0-*"
@ -14,7 +14,6 @@
"frameworks": { "frameworks": {
"dnxcore50": { "dnxcore50": {
"Microsoft.NETCore.Platforms": "1.0.0-*"
} }
} }
} }

View File

@ -6,13 +6,17 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests
{ {
public class WebConfigTransformFacts public class WebConfigTransformFacts
{ {
private XDocument WebConfigTemplate => XDocument.Parse( private XDocument WebConfigTemplate => XDocument.Parse(
@"<configuration> @"<configuration>
<system.webServer> <system.webServer>
<handlers> <handlers>
<add name=""httpPlatformHandler"" path=""*"" verb=""*"" modules=""httpPlatformHandler"" resourceType=""Unspecified""/> <add name=""httpPlatformHandler"" path=""*"" verb=""*"" modules=""httpPlatformHandler"" resourceType=""Unspecified""/>
</handlers> </handlers>
<httpPlatform processPath=""..\test.exe"" stdoutLogEnabled=""false"" stdoutLogFile=""..\logs\stdout.log"" startupTimeLimit=""3600""/> <httpPlatform processPath=""..\test.exe"" stdoutLogEnabled=""false"" stdoutLogFile=""..\logs\stdout.log"" startupTimeLimit=""3600"">
<environmentVariables>
<environmentVariable name=""ASPNET_APPLICATIONBASE"" value=""."" />
</environmentVariables>
</httpPlatform>
</system.webServer> </system.webServer>
</configuration>"); </configuration>");
@ -31,14 +35,16 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests
} }
[Theory] [Theory]
[InlineData(new object[] { new[] {"system.webServer"}})] [InlineData(new object[] { new[] { "system.webServer" } })]
[InlineData(new object[] { new[] {"add"}})] [InlineData(new object[] { new[] { "add" } })]
[InlineData(new object[] { new[] {"handlers"}})] [InlineData(new object[] { new[] { "handlers" } })]
[InlineData(new object[] { new[] {"httpPlatform"}})] [InlineData(new object[] { new[] { "httpPlatform" } })]
[InlineData(new object[] { new[] {"handlers", "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) public void WebConfigTransform_adds_missing_elements(string[] elementNames)
{ {
var input = new XDocument(WebConfigTemplate); var input = WebConfigTemplate;
foreach (var elementName in elementNames) foreach (var elementName in elementNames)
{ {
input.Descendants(elementName).Remove(); input.Descendants(elementName).Remove();
@ -50,7 +56,7 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests
[Theory] [Theory]
[InlineData("add", "path", "test")] [InlineData("add", "path", "test")]
[InlineData("add", "verb","test")] [InlineData("add", "verb", "test")]
[InlineData("add", "modules", "mods")] [InlineData("add", "modules", "mods")]
[InlineData("add", "resourceType", "Either")] [InlineData("add", "resourceType", "Either")]
[InlineData("httpPlatform", "stdoutLogEnabled", "true")] [InlineData("httpPlatform", "stdoutLogEnabled", "true")]
@ -59,7 +65,7 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests
[InlineData("httpPlatform", "stdoutLogFile", "logfile.log")] [InlineData("httpPlatform", "stdoutLogFile", "logfile.log")]
public void WebConfigTransform_wont_override_custom_values(string elementName, string attributeName, string attributeValue) 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); input.Descendants(elementName).Single().SetAttributeValue(attributeName, attributeValue);
var output = WebConfigTransform.Transform(input, "test.exe", configureForAzure: false); var output = WebConfigTransform.Transform(input, "test.exe", configureForAzure: false);
@ -79,7 +85,7 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests
[Fact] [Fact]
public void WebConfigTransform_fixes_httpPlatformHandler_casing() public void WebConfigTransform_fixes_httpPlatformHandler_casing()
{ {
var input = new XDocument(WebConfigTemplate); var input = WebConfigTemplate;
input.Descendants("add").Single().SetAttributeValue("name", "httpplatformhandler"); input.Descendants("add").Single().SetAttributeValue("name", "httpplatformhandler");
Assert.True(XNode.DeepEquals(WebConfigTemplate, Assert.True(XNode.DeepEquals(WebConfigTemplate,
@ -89,22 +95,21 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests
[Fact] [Fact]
public void WebConfigTransform_does_not_remove_children_of_httpPlatform_element() public void WebConfigTransform_does_not_remove_children_of_httpPlatform_element()
{ {
var envVarsElement = var envVarElement =
new XElement("environmentVariables", new XElement("environmentVariable", new XAttribute("name", "ENVVAR"), new XAttribute("value", "123"));
new XElement("environmentVariable", new XAttribute("name", "ENVVAR"), new XAttribute("value", "123")));
var input = new XDocument(WebConfigTemplate); var input = WebConfigTemplate;
input.Descendants("httpPlatform").Single().Add(envVarsElement); input.Descendants("environmentVariable").Single().Add(envVarElement);
Assert.True(XNode.DeepEquals(envVarsElement, Assert.True(XNode.DeepEquals(envVarElement,
WebConfigTransform.Transform(input, "app.exe", configureForAzure: false) WebConfigTransform.Transform(input, "app.exe", configureForAzure: false)
.Descendants("httpPlatform").Elements().Single())); .Descendants("environmentVariable").SingleOrDefault(e => (string)e.Attribute("name") == "ENVVAR")));
} }
[Fact] [Fact]
public void WebConfigTransform_adds_stdoutLogEnabled_if_attribute_is_missing() public void WebConfigTransform_adds_stdoutLogEnabled_if_attribute_is_missing()
{ {
var input = new XDocument(WebConfigTemplate); var input = WebConfigTemplate;
input.Descendants("httpPlatform").Attributes("stdoutLogEnabled").Remove(); input.Descendants("httpPlatform").Attributes("stdoutLogEnabled").Remove();
Assert.Equal( Assert.Equal(
@ -119,7 +124,7 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests
[InlineData("true")] [InlineData("true")]
public void WebConfigTransform_adds_stdoutLogFile_if_attribute_is_missing(string stdoutLogFile) 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(); var httpPlatformElement = input.Descendants("httpPlatform").Single();
httpPlatformElement.Attribute("stdoutLogEnabled").Remove(); httpPlatformElement.Attribute("stdoutLogEnabled").Remove();
@ -140,7 +145,7 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests
[InlineData("false")] [InlineData("false")]
public void WebConfigTransform_does_not_change_existing_stdoutLogEnabled(string stdoutLogEnabledValue) public void WebConfigTransform_does_not_change_existing_stdoutLogEnabled(string stdoutLogEnabledValue)
{ {
var input = new XDocument(WebConfigTemplate); var input = WebConfigTemplate;
var httpPlatformElement = input.Descendants("httpPlatform").Single(); var httpPlatformElement = input.Descendants("httpPlatform").Single();
httpPlatformElement.SetAttributeValue("stdoutLogFile", "mylog.txt"); httpPlatformElement.SetAttributeValue("stdoutLogFile", "mylog.txt");
@ -159,18 +164,31 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS.Tests
[Fact] [Fact]
public void WebConfigTransform_correctly_configures_for_Azure() public void WebConfigTransform_correctly_configures_for_Azure()
{ {
var input = new XDocument(WebConfigTemplate); var input = WebConfigTemplate;
input.Descendants("httpPlatform").Attributes().Remove(); input.Descendants("httpPlatform").Attributes().Remove();
var httPlatformElement = WebConfigTransform.Transform(input, "test.exe", configureForAzure: true)
.Descendants("httpPlatform").Single();
httPlatformElement.Elements().Remove();
Assert.True(XNode.DeepEquals( Assert.True(XNode.DeepEquals(
XDocument.Parse(@"<httpPlatform processPath=""%home%\site\test.exe"" stdoutLogEnabled=""false"" XDocument.Parse(@"<httpPlatform processPath=""%home%\site\test.exe"" stdoutLogEnabled=""false""
stdoutLogFile=""\\?\%home%\LogFiles\stdout.log"" startupTimeLimit=""3600""/>").Root, stdoutLogFile=""\\?\%home%\LogFiles\stdout.log"" startupTimeLimit=""3600""/>").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) private bool VerifyMissingElementCreated(params string[] elementNames)
{ {
var input = new XDocument(WebConfigTemplate); var input = WebConfigTemplate;
foreach (var elementName in elementNames) foreach (var elementName in elementNames)
{ {
input.Descendants(elementName).Remove(); input.Descendants(elementName).Remove();