question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebApplicationOptions.WebRootPath is ignored if a wwwroot folder exists

See original GitHub issue

Is 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:closed
  • Created 2 years ago
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
space-aliencommented, Mar 13, 2022

Here is an ultra-minimal repro that demonstrates the problem: https://github.com/space-alien/AspNetWebRootPath

0reactions
javiercncommented, Mar 28, 2022

Filed https://github.com/dotnet/AspNetCore.Docs/issues/25446 to improve the docs for this scenario.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP.NET Core WebApi wwwroot folder marked as ignored
When I learn project structure I found then folder for static files (wwwroot) marked as ignored. StaticFiles package was added as well as ......
Read more >
wwwroot folder in ASP.NET Core
In this article, I am going to discuss wwwroot folder in ASP.NET Core. The wwwroot folder is treated as the webroot folder in...
Read more >
Untitled
Webapplication.createbuilder does not exist WebMar 9, 2022 · Security 9 Insights New issue WebApplicationOptions.WebRootPath is ignored if a wwwroot folder ...
Read more >
Static files in ASP.NET Core
When IWebHostEnvironment.WebRootPath is set to a folder other than wwwroot : In the development environment, static assets found in both wwwroot ...
Read more >
[ASP.NET Core] 웹 루트(web root)를 변경하거나 시작 폴더 ...
참고 : github - dotnet/aspnetcore Issues : WebApplicationOptions.WebRootPath is ignored if a wwwroot folder exists.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found