#411 Move webroot from project.json to config.
This commit is contained in:
parent
39c355002b
commit
10822b3b4f
|
|
@ -3,20 +3,28 @@
|
|||
|
||||
using System.IO;
|
||||
using Microsoft.AspNet.FileProviders;
|
||||
using Microsoft.AspNet.Hosting.Internal;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Microsoft.AspNet.Hosting
|
||||
{
|
||||
public static class HostingEnvironmentExtensions
|
||||
{
|
||||
public static void Initialize(this IHostingEnvironment hostingEnvironment, string applicationBasePath, string environmentName)
|
||||
private const string OldEnvironmentKey = "ASPNET_ENV";
|
||||
private const string EnvironmentKey = "Hosting:Environment";
|
||||
|
||||
private const string WebRootKey = "Hosting:WebRoot";
|
||||
|
||||
public static void Initialize(this IHostingEnvironment hostingEnvironment, string applicationBasePath, IConfiguration config)
|
||||
{
|
||||
hostingEnvironment.WebRootPath = HostingUtilities.GetWebRoot(applicationBasePath);
|
||||
var webRoot = config?[WebRootKey] ?? string.Empty;
|
||||
hostingEnvironment.WebRootPath = Path.Combine(applicationBasePath, webRoot);
|
||||
if (!Directory.Exists(hostingEnvironment.WebRootPath))
|
||||
{
|
||||
Directory.CreateDirectory(hostingEnvironment.WebRootPath);
|
||||
}
|
||||
hostingEnvironment.WebRootFileProvider = new PhysicalFileProvider(hostingEnvironment.WebRootPath);
|
||||
|
||||
var environmentName = config?[EnvironmentKey] ?? config?[OldEnvironmentKey];
|
||||
hostingEnvironment.EnvironmentName = environmentName ?? hostingEnvironment.EnvironmentName;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,31 +0,0 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Microsoft.AspNet.Hosting.Internal
|
||||
{
|
||||
public static class HostingUtilities
|
||||
{
|
||||
public static string GetWebRoot(string applicationBasePath)
|
||||
{
|
||||
var webroot = applicationBasePath;
|
||||
using (var stream = File.OpenRead(Path.Combine(applicationBasePath, "project.json")))
|
||||
{
|
||||
using (var reader = new JsonTextReader(new StreamReader(stream)))
|
||||
{
|
||||
var project = JObject.Load(reader);
|
||||
JToken token;
|
||||
if (project.TryGetValue("webroot", out token))
|
||||
{
|
||||
webroot = Path.Combine(applicationBasePath, token.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
return Path.GetFullPath(webroot);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -21,9 +21,6 @@ namespace Microsoft.AspNet.Hosting
|
|||
{
|
||||
public class WebHostBuilder
|
||||
{
|
||||
public const string OldEnvironmentKey = "ASPNET_ENV";
|
||||
public const string EnvironmentKey = "Hosting:Environment";
|
||||
|
||||
public const string OldApplicationKey = "app";
|
||||
public const string ApplicationKey = "Hosting:Application";
|
||||
|
||||
|
|
@ -115,7 +112,7 @@ namespace Microsoft.AspNet.Hosting
|
|||
var appEnvironment = hostingContainer.GetRequiredService<IApplicationEnvironment>();
|
||||
var startupLoader = hostingContainer.GetRequiredService<IStartupLoader>();
|
||||
|
||||
_hostingEnvironment.Initialize(appEnvironment.ApplicationBasePath, _config?[EnvironmentKey] ?? _config?[OldEnvironmentKey]);
|
||||
_hostingEnvironment.Initialize(appEnvironment.ApplicationBasePath, _config);
|
||||
var engine = new HostingEngine(hostingServices, startupLoader, _config, _captureStartupErrors);
|
||||
|
||||
// Only one of these should be set, but they are used in priority
|
||||
|
|
|
|||
|
|
@ -23,7 +23,6 @@
|
|||
"Microsoft.Extensions.Logging": "1.0.0-*",
|
||||
"Microsoft.Dnx.Compilation.Abstractions": "1.0.0-*",
|
||||
"Microsoft.Dnx.Runtime.Abstractions": "1.0.0-*",
|
||||
"Newtonsoft.Json": "6.0.6",
|
||||
"System.Diagnostics.DiagnosticSource": "4.0.0-beta-*"
|
||||
},
|
||||
"frameworks": {
|
||||
|
|
|
|||
|
|
@ -248,9 +248,18 @@ namespace Microsoft.AspNet.Hosting
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public void WebRootCanBeResolvedFromTheProjectJson()
|
||||
public void WebRootCanBeResolvedFromTheConfig()
|
||||
{
|
||||
var engine = CreateBuilder().UseServer(this).Build();
|
||||
var vals = new Dictionary<string, string>
|
||||
{
|
||||
{ "Hosting:WebRoot", "testroot" }
|
||||
};
|
||||
|
||||
var builder = new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(vals);
|
||||
var config = builder.Build();
|
||||
|
||||
var engine = CreateBuilder(config).UseServer(this).Build();
|
||||
var env = engine.ApplicationServices.GetRequiredService<IHostingEnvironment>();
|
||||
Assert.Equal(Path.GetFullPath("testroot"), env.WebRootPath);
|
||||
Assert.True(env.WebRootFileProvider.GetFileInfo("TextFile.txt").Exists);
|
||||
|
|
|
|||
|
|
@ -1,18 +0,0 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using Microsoft.AspNet.Hosting.Internal;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Hosting.Tests
|
||||
{
|
||||
public class HostingUtilitiesTests
|
||||
{
|
||||
[Fact]
|
||||
public void ReadWebRootFromProjectJson()
|
||||
{
|
||||
var root = HostingUtilities.GetWebRoot(".");
|
||||
Assert.True(root.EndsWith("testroot"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -13,6 +13,5 @@
|
|||
},
|
||||
"commands": {
|
||||
"test": "xunit.runner.aspnet"
|
||||
},
|
||||
"webroot": "testroot"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -344,7 +344,7 @@ namespace Microsoft.AspNet.TestHost
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public void WebRootCanBeResolvedWhenNotInTheProjectJson()
|
||||
public void WebRootCanBeResolvedWhenNotInTheConfig()
|
||||
{
|
||||
TestServer server = TestServer.Create(app =>
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue