From 79df7c9ca74376286a4f1531102a80caabaa0a85 Mon Sep 17 00:00:00 2001 From: moozzyk Date: Fri, 29 Jan 2016 18:03:52 -0800 Subject: [PATCH] Resolving relative application base paths --- .../WebHostBuilder.cs | 24 ++++++++++++++++--- .../WebHostBuilderTests.cs | 15 +++++++++++- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs b/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs index 41dc917029..33d3e7cc11 100644 --- a/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs +++ b/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs @@ -3,6 +3,7 @@ using System; using System.Diagnostics; +using System.IO; using System.Runtime.Versioning; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting.Builder; @@ -84,7 +85,7 @@ namespace Microsoft.AspNetCore.Hosting } /// - /// Specify the startup type to be used by the web host. + /// Specify the startup type to be used by the web host. /// /// The to be used. /// The . @@ -111,7 +112,7 @@ namespace Microsoft.AspNetCore.Hosting } /// - /// Specify the startup method to be used to configure the web application. + /// Specify the startup method to be used to configure the web application. /// /// The delegate that configures the . /// The . @@ -224,7 +225,7 @@ namespace Microsoft.AspNetCore.Hosting { public WrappedApplicationEnvironment(string applicationBasePath, IApplicationEnvironment env) { - ApplicationBasePath = applicationBasePath; + ApplicationBasePath = ResolvePath(applicationBasePath); ApplicationName = env.ApplicationName; ApplicationVersion = env.ApplicationVersion; RuntimeFramework = env.RuntimeFramework; @@ -238,5 +239,22 @@ namespace Microsoft.AspNetCore.Hosting public FrameworkName RuntimeFramework { get; } } + + private static string ResolvePath(string applicationBasePath) + { + if (Path.IsPathRooted(applicationBasePath)) + { + return applicationBasePath; + } + + var basePath = +#if NET451 + (string)AppDomain.CurrentDomain.GetData("APP_CONTEXT_BASE_DIRECTORY") ?? + AppDomain.CurrentDomain.BaseDirectory; +#else + AppContext.BaseDirectory; +#endif + return Path.Combine(Path.GetFullPath(basePath), applicationBasePath); + } } } diff --git a/test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs b/test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs index a3f60e73a3..9e9f7a351c 100644 --- a/test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs +++ b/test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs @@ -155,7 +155,7 @@ namespace Microsoft.AspNetCore.Hosting .UseSetting(WebHostDefaults.ServerKey, "ServerB") .UseServer(new TestServer()) .UseStartup(); - + var host = (WebHost)hostBuilder.Build(); Assert.Equal("ServerB", host.ServerFactoryLocation); @@ -304,6 +304,19 @@ namespace Microsoft.AspNetCore.Hosting Assert.Equal("/foo/bar", host.Services.GetService().ApplicationBasePath); } + [Fact] + public void RelativeApplicationBaseAreResolved() + { + var builder = new ConfigurationBuilder(); + var host = new WebHostBuilder() + .UseApplicationBasePath("bar") + .UseServer(new TestServer()) + .UseStartup("Microsoft.AspNetCore.Hosting.Tests") + .Build(); + + Assert.Equal(Path.GetFullPath("bar"), host.Services.GetService().ApplicationBasePath); + } + private IWebHostBuilder CreateWebHostBuilder() { var vals = new Dictionary