15

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"
    ]
},

3 Answers 3

17

Is it possible to configure a different folder to replace wwwroot in ASP.NET Core?

Yes. Add a UseWebRoot call in your Program class:

public static void Main(string[] args)
{
    var host = new WebHostBuilder()
        .UseKestrel()
        .UseWebRoot("myroot") // name it whatever you want
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

are there any side effects to this change?

Here are three that I can think of:

  1. The Bower package manager will not work properly, as it looks for a lib folder in wwwroot. I'm not certain if this is configurable.
  2. You will need to fix bundleconfig.json to look at the new directory.
  3. You will need to update the include portion in project.json to include your new directory in the publish output.
1
  • 1
    Regarding #1, the path for Bower can be configured in the .bowerrc file that should be in the root folder for the project. This is what's there by default: { "directory": "wwwroot/lib" }
    – Jason
    Commented Dec 19, 2016 at 17:41
13

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)

4

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.