WebApplicationOptions.WebRootPath is ignored if a wwwroot folder exists
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Describe the bug
If a wwwroot
folder exists, content is served from that location even though WebApplicationOptions.WebRootPath
has been specified.
In my development environment, I want to serve my web app from uncompiled sources. In production, my web app is compiled into the wwwroot
folder. I switch the web root path on the development environment variable as shown below. (This is the only way I could find in .NET 6, whereas before it was trivial to set the IWebHostEnvironment.WebRootPath
property).
I am now forced to rename the production wwwroot
folder to something else, in order to circumvent this default behaviour.
WebApplicationBuilder builder;
{
WebApplicationOptions opts;
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == Environments.Development)
{
var path = Path.Combine(
Directory.GetParent(Environment.CurrentDirectory.TrimEnd('\\'))!.ToString(),
"MyWebAppDevSources");
opts = new()
{
Args = args,
WebRootPath = path,
};
}
else
{
opts = new()
{
Args = args,
WebRootPath = "webroot", // Forced to use a different location in Production,
// just to avoid having requests being served from the
// `wwwroot` folder in Development mode.
};
}
builder = WebApplication.CreateBuilder(opts);
}
Expected Behavior
I expect the WebApplicationOptions.WebRootPath
property to always take precedence even when there is a default wwwroot
folder on disk.
I also wish there were a way to set this property after instantiating the WebApplicationBuilder
.
Steps To Reproduce
No response
Exceptions (if any)
No response
.NET Version
6.0.200
Anything else?
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (9 by maintainers)
Top GitHub Comments
Here is an ultra-minimal repro that demonstrates the problem: https://github.com/space-alien/AspNetWebRootPath
Filed https://github.com/dotnet/AspNetCore.Docs/issues/25446 to improve the docs for this scenario.