Skip to content

Commit fd6ea53

Browse files
authored
[release/6.0] Fix diff checks for ContentRoot and WebRoot (#41182)
1 parent 1349707 commit fd6ea53

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

src/DefaultBuilder/src/ConfigureHostBuilder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public IHostBuilder ConfigureHostConfiguration(Action<IConfigurationBuilder> con
6262
var previousApplicationName = _configuration[HostDefaults.ApplicationKey];
6363
// Use the real content root so we can compare paths
6464
var previousContentRoot = _context.HostingEnvironment.ContentRootPath;
65+
var previousContentRootConfig = _configuration[HostDefaults.ContentRootKey];
6566
var previousEnvironment = _configuration[HostDefaults.EnvironmentKey];
6667

6768
// Run these immediately so that they are observable by the imperative code
@@ -74,7 +75,8 @@ public IHostBuilder ConfigureHostConfiguration(Action<IConfigurationBuilder> con
7475
throw new NotSupportedException($"The application name changed from \"{previousApplicationName}\" to \"{_configuration[HostDefaults.ApplicationKey]}\". Changing the host configuration using WebApplicationBuilder.Host is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.");
7576
}
7677

77-
if (!string.Equals(previousContentRoot, HostingPathResolver.ResolvePath(_configuration[HostDefaults.ContentRootKey]), StringComparison.OrdinalIgnoreCase))
78+
if (!string.Equals(previousContentRootConfig, _configuration[HostDefaults.ContentRootKey], StringComparison.OrdinalIgnoreCase)
79+
&& !string.Equals(previousContentRoot, HostingPathResolver.ResolvePath(_configuration[HostDefaults.ContentRootKey]), StringComparison.OrdinalIgnoreCase))
7880
{
7981
throw new NotSupportedException($"The content root changed from \"{previousContentRoot}\" to \"{HostingPathResolver.ResolvePath(_configuration[HostDefaults.ContentRootKey])}\". Changing the host configuration using WebApplicationBuilder.Host is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.");
8082
}

src/DefaultBuilder/src/ConfigureWebHostBuilder.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ IWebHost IWebHostBuilder.Build()
3939
public IWebHostBuilder ConfigureAppConfiguration(Action<WebHostBuilderContext, IConfigurationBuilder> configureDelegate)
4040
{
4141
var previousContentRoot = _context.HostingEnvironment.ContentRootPath;
42+
var previousContentRootConfig = _configuration[WebHostDefaults.ContentRootKey];
4243
var previousWebRoot = _context.HostingEnvironment.WebRootPath;
44+
var previousWebRootConfig = _configuration[WebHostDefaults.WebRootKey];
4345
var previousApplication = _configuration[WebHostDefaults.ApplicationKey];
4446
var previousEnvironment = _configuration[WebHostDefaults.EnvironmentKey];
4547
var previousHostingStartupAssemblies = _configuration[WebHostDefaults.HostingStartupAssembliesKey];
@@ -48,7 +50,8 @@ public IWebHostBuilder ConfigureAppConfiguration(Action<WebHostBuilderContext, I
4850
// Run these immediately so that they are observable by the imperative code
4951
configureDelegate(_context, _configuration);
5052

51-
if (!string.Equals(HostingPathResolver.ResolvePath(previousWebRoot, previousContentRoot), HostingPathResolver.ResolvePath(_configuration[WebHostDefaults.WebRootKey], previousContentRoot), StringComparison.OrdinalIgnoreCase))
53+
if (!string.Equals(previousWebRootConfig, _configuration[WebHostDefaults.WebRootKey], StringComparison.OrdinalIgnoreCase)
54+
&& !string.Equals(HostingPathResolver.ResolvePath(previousWebRoot, previousContentRoot), HostingPathResolver.ResolvePath(_configuration[WebHostDefaults.WebRootKey], previousContentRoot), StringComparison.OrdinalIgnoreCase))
5255
{
5356
// Diasllow changing the web root for consistency with other types.
5457
throw new NotSupportedException($"The web root changed from \"{HostingPathResolver.ResolvePath(previousWebRoot, previousContentRoot)}\" to \"{HostingPathResolver.ResolvePath(_configuration[WebHostDefaults.WebRootKey], previousContentRoot)}\". Changing the host configuration using WebApplicationBuilder.WebHost is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.");
@@ -58,7 +61,8 @@ public IWebHostBuilder ConfigureAppConfiguration(Action<WebHostBuilderContext, I
5861
// Disallow changing any host configuration
5962
throw new NotSupportedException($"The application name changed from \"{previousApplication}\" to \"{_configuration[WebHostDefaults.ApplicationKey]}\". Changing the host configuration using WebApplicationBuilder.WebHost is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.");
6063
}
61-
else if (!string.Equals(previousContentRoot, HostingPathResolver.ResolvePath(_configuration[WebHostDefaults.ContentRootKey]), StringComparison.OrdinalIgnoreCase))
64+
else if (!string.Equals(previousContentRootConfig, _configuration[WebHostDefaults.ContentRootKey], StringComparison.OrdinalIgnoreCase)
65+
&& !string.Equals(previousContentRoot, HostingPathResolver.ResolvePath(_configuration[WebHostDefaults.ContentRootKey]), StringComparison.OrdinalIgnoreCase))
6266
{
6367
// Disallow changing any host configuration
6468
throw new NotSupportedException($"The content root changed from \"{previousContentRoot}\" to \"{HostingPathResolver.ResolvePath(_configuration[WebHostDefaults.ContentRootKey])}\". Changing the host configuration using WebApplicationBuilder.WebHost is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.");

src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,36 @@ public async Task NoExceptionAreThrownForBadRequestsInProduction()
16861686
Assert.Equal(string.Empty, responseBody);
16871687
}
16881688

1689+
[Fact]
1690+
public void EmptyAppConfiguration()
1691+
{
1692+
var wwwroot = Path.Combine(AppContext.BaseDirectory, "wwwroot");
1693+
bool createdDirectory = false;
1694+
if (!Directory.Exists(wwwroot))
1695+
{
1696+
createdDirectory = true;
1697+
Directory.CreateDirectory(wwwroot);
1698+
}
1699+
1700+
try
1701+
{
1702+
var builder = WebApplication.CreateBuilder();
1703+
1704+
builder.WebHost.ConfigureAppConfiguration((ctx, config) => { });
1705+
1706+
using var app = builder.Build();
1707+
var hostEnv = app.Services.GetRequiredService<Hosting.IWebHostEnvironment>();
1708+
Assert.Equal(wwwroot, hostEnv.WebRootPath);
1709+
}
1710+
finally
1711+
{
1712+
if (createdDirectory)
1713+
{
1714+
Directory.Delete(wwwroot);
1715+
}
1716+
}
1717+
}
1718+
16891719
[Fact]
16901720
public void HostConfigurationNotAffectedByConfiguration()
16911721
{

0 commit comments

Comments
 (0)