Add the ability to set the IApplicationEnvironment.ApplicationBasePath

- Added UseApplicationBasePath which sets the base path (used for views
and static files)
This commit is contained in:
David Fowler 2016-01-04 17:43:23 -08:00
parent d8a20521f1
commit 46e3e25ec7
3 changed files with 59 additions and 1 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.IO;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.DependencyInjection;
@ -14,6 +15,7 @@ namespace SampleStartups
{
var application = new WebApplicationBuilder()
.UseServerFactory("Microsoft.AspNet.Server.Kestrel") // Set the server manually
.UseApplicationBasePath(Directory.GetCurrentDirectory()) // Override the application base with the current directory
.UseEnvironment("Development")
.UseWebRoot("public")
.ConfigureLogging(loggerFactory =>

View File

@ -3,6 +3,7 @@
using System;
using System.Diagnostics;
using System.Runtime.Versioning;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting.Builder;
using Microsoft.AspNet.Hosting.Internal;
@ -29,6 +30,7 @@ namespace Microsoft.AspNet.Hosting
private Action<IServiceCollection> _configureServices;
private string _environmentName;
private string _webRoot;
private string _applicationBasePath;
// Only one of these should be set
private StartupMethods _startup;
@ -52,6 +54,12 @@ namespace Microsoft.AspNet.Hosting
return this;
}
public WebApplicationBuilder UseApplicationBasePath(string applicationBasePath)
{
_applicationBasePath = applicationBasePath;
return this;
}
public WebApplicationBuilder UseEnvironment(string environment)
{
if (environment == null)
@ -216,11 +224,18 @@ namespace Microsoft.AspNet.Hosting
services.AddTransient<IStartupFilter, AutoRequestServicesStartupFilter>();
var defaultPlatformServices = PlatformServices.Default;
if (defaultPlatformServices != null)
{
if (defaultPlatformServices.Application != null)
{
services.TryAddSingleton(defaultPlatformServices.Application);
var appEnv = defaultPlatformServices.Application;
if (!string.IsNullOrEmpty(_applicationBasePath))
{
appEnv = new WrappedApplicationEnvironment(_applicationBasePath, appEnv);
}
services.TryAddSingleton(appEnv);
}
if (defaultPlatformServices.Runtime != null)
@ -236,5 +251,24 @@ namespace Microsoft.AspNet.Hosting
return services;
}
private class WrappedApplicationEnvironment : IApplicationEnvironment
{
public WrappedApplicationEnvironment(string applicationBasePath, IApplicationEnvironment env)
{
ApplicationBasePath = applicationBasePath;
ApplicationName = env.ApplicationName;
ApplicationVersion = env.ApplicationVersion;
RuntimeFramework = env.RuntimeFramework;
}
public string ApplicationBasePath { get; }
public string ApplicationName { get; }
public string ApplicationVersion { get; }
public FrameworkName RuntimeFramework { get; }
}
}
}

View File

@ -13,6 +13,7 @@ using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Http.Internal;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.PlatformAbstractions;
using Xunit;
namespace Microsoft.AspNet.Hosting
@ -137,6 +138,27 @@ namespace Microsoft.AspNet.Hosting
Assert.Equal(expected, application.Services.GetService<IHostingEnvironment>().EnvironmentName);
}
[Fact]
public void UseBasePathConfiguresBasePath()
{
var vals = new Dictionary<string, string>
{
{ "ENV", "Dev" },
};
var builder = new ConfigurationBuilder()
.AddInMemoryCollection(vals);
var config = builder.Build();
var application = new WebApplicationBuilder()
.UseConfiguration(config)
.UseApplicationBasePath("/foo/bar")
.UseServer(new TestServer())
.UseStartup("Microsoft.AspNet.Hosting.Tests")
.Build();
Assert.Equal("/foo/bar", application.Services.GetService<IApplicationEnvironment>().ApplicationBasePath);
}
private WebApplicationBuilder CreateWebApplicationBuilder()
{
var vals = new Dictionary<string, string>