Resolving relative application base paths

This commit is contained in:
moozzyk 2016-01-29 18:03:52 -08:00
parent 906ed5f0fb
commit 79df7c9ca7
2 changed files with 35 additions and 4 deletions

View File

@ -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
}
/// <summary>
/// Specify the startup type to be used by the web host.
/// Specify the startup type to be used by the web host.
/// </summary>
/// <param name="startupType">The <see cref="Type"/> to be used.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
@ -111,7 +112,7 @@ namespace Microsoft.AspNetCore.Hosting
}
/// <summary>
/// Specify the startup method to be used to configure the web application.
/// Specify the startup method to be used to configure the web application.
/// </summary>
/// <param name="configureApplication">The delegate that configures the <see cref="IApplicationBuilder"/>.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
@ -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);
}
}
}

View File

@ -155,7 +155,7 @@ namespace Microsoft.AspNetCore.Hosting
.UseSetting(WebHostDefaults.ServerKey, "ServerB")
.UseServer(new TestServer())
.UseStartup<StartupNoServices>();
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<IApplicationEnvironment>().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<IApplicationEnvironment>().ApplicationBasePath);
}
private IWebHostBuilder CreateWebHostBuilder()
{
var vals = new Dictionary<string, string>