'How to Configure an Alternate Folder to wwwroot in ASP.NET Core?

Is it possible to configure a different folder to replace wwwroot in ASP.NET Core? And, if yes, how? And are there any side effects to this change?

The only config that currently includes wwwroot in the entire project is found in project.json as seen in the code below; but replacing the value with the name of the new folder is not enough for the static file (ex: index.html) to be read.

"publishOptions": {
    "include": [
        "wwwroot",
        "web.config"
    ]
},


Solution 1:[1]

With ASP.NET Core 2.2, in your Startup's Configure() method, you can change the following:

app.UseStaticFiles();

to

app.UseStaticFiles(new StaticFileOptions
{
  FileProvider = new PhysicalFileProvider(Path.Combine(
    AppDomain.CurrentDomain.BaseDirectory,
    "myStaticFolder")),
});

Reference / Source (auch auf Deutsch)

Solution 2:[2]

With ASP.NET Core 6, if you're using the new minimal hosting model with the WebApplication class, then the simplest approach is to configure this via the WebRootPath property of the WebApplicationOptions class:

var builder = WebApplication.CreateBuilder(
    new WebApplicationOptions() 
    {
        WebRootPath = "mywebroot"
    }
);

The minimal hosting model is configured by default in web applications first generated using the Visual Studio 2022+ or .NET 6 SDK templates, so this will likely be the most familiar approach for new ASP.NET Core 6 applications.

While the entry point has changed with WebApplication, it’s still technically possible to get to UseWebRoot(), as recommended in the accepted answer, via the minimal hosting model, but calling it will produce a warning recommending the use of WebApplicationOptions instead.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeremy Caney
Solution 2