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;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Runtime.Versioning; using System.Runtime.Versioning;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting.Builder; using Microsoft.AspNetCore.Hosting.Builder;
@ -224,7 +225,7 @@ namespace Microsoft.AspNetCore.Hosting
{ {
public WrappedApplicationEnvironment(string applicationBasePath, IApplicationEnvironment env) public WrappedApplicationEnvironment(string applicationBasePath, IApplicationEnvironment env)
{ {
ApplicationBasePath = applicationBasePath; ApplicationBasePath = ResolvePath(applicationBasePath);
ApplicationName = env.ApplicationName; ApplicationName = env.ApplicationName;
ApplicationVersion = env.ApplicationVersion; ApplicationVersion = env.ApplicationVersion;
RuntimeFramework = env.RuntimeFramework; RuntimeFramework = env.RuntimeFramework;
@ -238,5 +239,22 @@ namespace Microsoft.AspNetCore.Hosting
public FrameworkName RuntimeFramework { get; } 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

@ -304,6 +304,19 @@ namespace Microsoft.AspNetCore.Hosting
Assert.Equal("/foo/bar", host.Services.GetService<IApplicationEnvironment>().ApplicationBasePath); 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() private IWebHostBuilder CreateWebHostBuilder()
{ {
var vals = new Dictionary<string, string> var vals = new Dictionary<string, string>