From 46e3e25ec7c5212029b64a25efbe1f11cba2afc5 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Mon, 4 Jan 2016 17:43:23 -0800 Subject: [PATCH] Add the ability to set the IApplicationEnvironment.ApplicationBasePath - Added UseApplicationBasePath which sets the base path (used for views and static files) --- samples/SampleStartups/StartupFullControl.cs | 2 ++ .../WebApplicationBuilder.cs | 36 ++++++++++++++++++- .../WebApplicationBuilderTests.cs | 22 ++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/samples/SampleStartups/StartupFullControl.cs b/samples/SampleStartups/StartupFullControl.cs index 34a00a5a46..82c32791d6 100644 --- a/samples/SampleStartups/StartupFullControl.cs +++ b/samples/SampleStartups/StartupFullControl.cs @@ -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 => diff --git a/src/Microsoft.AspNet.Hosting/WebApplicationBuilder.cs b/src/Microsoft.AspNet.Hosting/WebApplicationBuilder.cs index 12e595c50d..bec3ee4381 100644 --- a/src/Microsoft.AspNet.Hosting/WebApplicationBuilder.cs +++ b/src/Microsoft.AspNet.Hosting/WebApplicationBuilder.cs @@ -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 _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(); 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; } + } } } diff --git a/test/Microsoft.AspNet.Hosting.Tests/WebApplicationBuilderTests.cs b/test/Microsoft.AspNet.Hosting.Tests/WebApplicationBuilderTests.cs index edeaffb499..06f912ac17 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/WebApplicationBuilderTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/WebApplicationBuilderTests.cs @@ -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().EnvironmentName); } + [Fact] + public void UseBasePathConfiguresBasePath() + { + var vals = new Dictionary + { + { "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().ApplicationBasePath); + } + private WebApplicationBuilder CreateWebApplicationBuilder() { var vals = new Dictionary