Remove webroot fallback to approot
This commit is contained in:
parent
73fd257844
commit
6a719ab95d
|
|
@ -91,7 +91,10 @@ namespace Microsoft.AspNet.Hosting
|
|||
{
|
||||
throw new ArgumentNullException(nameof(hostingEnvironment));
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(hostingEnvironment.WebRootPath))
|
||||
{
|
||||
throw new InvalidOperationException("Can not map path because webroot path is not set");
|
||||
}
|
||||
if (virtualPath == null)
|
||||
{
|
||||
return hostingEnvironment.WebRootPath;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using Microsoft.AspNet.FileProviders;
|
||||
using Microsoft.AspNet.Hosting.Internal;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Microsoft.AspNet.Hosting
|
||||
|
|
@ -26,24 +27,24 @@ namespace Microsoft.AspNet.Hosting
|
|||
{
|
||||
hostingEnvironment.WebRootPath = wwwroot;
|
||||
}
|
||||
else
|
||||
{
|
||||
hostingEnvironment.WebRootPath = applicationBasePath;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
hostingEnvironment.WebRootPath = Path.Combine(applicationBasePath, webRoot);
|
||||
}
|
||||
|
||||
hostingEnvironment.WebRootPath = Path.GetFullPath(hostingEnvironment.WebRootPath);
|
||||
|
||||
if (!Directory.Exists(hostingEnvironment.WebRootPath))
|
||||
if (!string.IsNullOrEmpty(hostingEnvironment.WebRootPath))
|
||||
{
|
||||
Directory.CreateDirectory(hostingEnvironment.WebRootPath);
|
||||
hostingEnvironment.WebRootPath = Path.GetFullPath(hostingEnvironment.WebRootPath);
|
||||
if (!Directory.Exists(hostingEnvironment.WebRootPath))
|
||||
{
|
||||
Directory.CreateDirectory(hostingEnvironment.WebRootPath);
|
||||
}
|
||||
hostingEnvironment.WebRootFileProvider = new PhysicalFileProvider(hostingEnvironment.WebRootPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
hostingEnvironment.WebRootFileProvider = new NullFileProvider();
|
||||
}
|
||||
hostingEnvironment.WebRootFileProvider = new PhysicalFileProvider(hostingEnvironment.WebRootPath);
|
||||
|
||||
var environmentName = options.Environment;
|
||||
hostingEnvironment.EnvironmentName = environmentName ?? hostingEnvironment.EnvironmentName;
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,82 @@
|
|||
// 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.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Microsoft.AspNet.FileProviders;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
|
||||
namespace Microsoft.AspNet.Hosting.Internal
|
||||
{
|
||||
internal class NullFileProvider : IFileProvider
|
||||
{
|
||||
public IDirectoryContents GetDirectoryContents(string subpath)
|
||||
{
|
||||
return new NullDirectoryContents();
|
||||
}
|
||||
|
||||
public IFileInfo GetFileInfo(string subpath)
|
||||
{
|
||||
return new NullFileInfo(subpath);
|
||||
}
|
||||
|
||||
public IChangeToken Watch(string filter)
|
||||
{
|
||||
return new NullChangeToken();
|
||||
}
|
||||
|
||||
private class NullDirectoryContents : IDirectoryContents
|
||||
{
|
||||
public bool Exists => false;
|
||||
|
||||
public IEnumerator<IFileInfo> GetEnumerator()
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
}
|
||||
|
||||
internal class NullFileInfo : IFileInfo
|
||||
{
|
||||
public NullFileInfo(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public bool Exists => false;
|
||||
|
||||
public bool IsDirectory => false;
|
||||
|
||||
public DateTimeOffset LastModified => DateTimeOffset.MinValue;
|
||||
|
||||
public long Length => -1;
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public string PhysicalPath => null;
|
||||
|
||||
public Stream CreateReadStream()
|
||||
{
|
||||
throw new FileNotFoundException(string.Format($"{nameof(NullFileProvider)} does not support reading files.", Name));
|
||||
}
|
||||
}
|
||||
|
||||
private class NullChangeToken : IChangeToken
|
||||
{
|
||||
public bool HasChanged => false;
|
||||
|
||||
public bool ActiveChangeCallbacks => false;
|
||||
|
||||
public IDisposable RegisterChangeCallback(Action<object> callback, object state)
|
||||
{
|
||||
throw new NotSupportedException($"{nameof(NullFileProvider)} does not support registering change notifications.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -464,6 +464,9 @@ namespace Microsoft.AspNet.Hosting
|
|||
using (engine.Start())
|
||||
{
|
||||
var env = engine.ApplicationServices.GetRequiredService<IHostingEnvironment>();
|
||||
// MapPath requires webroot to be set, we don't care
|
||||
// about file provider so just set it here
|
||||
env.WebRootPath = "";
|
||||
var mappedPath = env.MapPath(virtualPath);
|
||||
expectedSuffix = expectedSuffix.Replace('/', Path.DirectorySeparatorChar);
|
||||
Assert.Equal(Path.Combine(env.WebRootPath, expectedSuffix), mappedPath);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,71 @@
|
|||
// 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.IO;
|
||||
using Microsoft.AspNet.FileProviders;
|
||||
using Microsoft.AspNet.Hosting;
|
||||
using Microsoft.AspNet.Hosting.Internal;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Hosting.Tests
|
||||
{
|
||||
public class HostingEnvironmentExtensionsTests
|
||||
{
|
||||
[Fact]
|
||||
public void SetsFullPathToWwwroot()
|
||||
{
|
||||
var env = new HostingEnvironment();
|
||||
|
||||
env.Initialize(".", new WebHostOptions() {WebRoot = "testroot"}, null);
|
||||
|
||||
Assert.Equal(Path.GetFullPath("testroot"), env.WebRootPath);
|
||||
Assert.IsAssignableFrom<PhysicalFileProvider>(env.WebRootFileProvider);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DefaultsToWwwrootSubdir()
|
||||
{
|
||||
var env = new HostingEnvironment();
|
||||
|
||||
env.Initialize("testroot", new WebHostOptions(), null);
|
||||
|
||||
Assert.Equal(Path.GetFullPath(Path.Combine("testroot","wwwroot")), env.WebRootPath);
|
||||
Assert.IsAssignableFrom<PhysicalFileProvider>(env.WebRootFileProvider);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DefaultsToNullFileProvider()
|
||||
{
|
||||
var env = new HostingEnvironment();
|
||||
|
||||
env.Initialize(Path.Combine("testroot", "wwwroot"), new WebHostOptions(), null);
|
||||
|
||||
Assert.Null(env.WebRootPath);
|
||||
Assert.IsAssignableFrom<NullFileProvider>(env.WebRootFileProvider);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetsConfiguration()
|
||||
{
|
||||
var config = new ConfigurationBuilder().Build();
|
||||
var env = new HostingEnvironment();
|
||||
|
||||
env.Initialize(".", new WebHostOptions(), config);
|
||||
|
||||
Assert.Same(config, env.Configuration);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OverridesEnvironmentFromConfig()
|
||||
{
|
||||
var env = new HostingEnvironment();
|
||||
env.EnvironmentName = "SomeName";
|
||||
|
||||
env.Initialize(".", new WebHostOptions() { Environment = "NewName" }, null);
|
||||
|
||||
Assert.Equal("NewName", env.EnvironmentName);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
This file is here to keep directories needed by tests. Do no remove it.
|
||||
|
|
@ -305,16 +305,6 @@ namespace Microsoft.AspNet.TestHost
|
|||
Assert.Equal("CreateInvokesApp", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WebRootCanBeResolvedWhenNotInTheConfig()
|
||||
{
|
||||
TestServer server = TestServer.Create(app =>
|
||||
{
|
||||
var env = app.ApplicationServices.GetRequiredService<IHostingEnvironment>();
|
||||
Assert.Equal(Directory.GetCurrentDirectory(), env.WebRootPath);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DisposeStreamIgnored()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue