I’m working on an ASP.NET Core application and need to configure it to serve static files from multiple directories. I’m encountering unexpected behavior where files placed in the original wwwroot folder are still being served even though I’ve renamed wwwroot and configured new directories.
Here’s what I’ve done:
- Renamed wwwroot to myRoot:
I created a folder named myRoot and set it as the WebRootPath in the application configuration.
var builder = WebApplication.CreateBuilder(new WebApplicationOptions()
{
WebRootPath = "myRoot"
});
var app = builder.Build();
// Enable serving static files from myRoot
app.UseStaticFiles();
- Added a new folder named myWebRoot:
I added another folder called myWebRoot to serve static files from.
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "myWebRoot")),
RequestPath = "/myWebRoot"
});
Expected Behavior:
- Files should be served from myRoot and myWebRoot as configured.
- Files from the original wwwroot folder should not be served unless explicitly included.
Actual Behavior:
- Despite renaming wwwroot to myRoot and setting up myWebRoot, files placed in the original wwwroot directory are still being served.
Questions:
- Is there a default behavior in ASP.NET Core that allows serving files from the old wwwroot directory even after renaming it?
- How can I ensure that files are served only from the specified directories (myRoot and myWebRoot) and not from the old wwwroot folder?
- Are there any additional configurations or settings I should check to resolve this issue?
Additional Information:
- ASP.NET Core version: .Net 8.0
- Development environment: Visual Studio
Code and Screenshots:
Here’s a screenshot showing the folder structure and configuration
Full Code:
using Microsoft.Extensions.FileProviders;
var builder = WebApplication.CreateBuilder(new WebApplicationOptions()
{
WebRootPath = "myRoot"
});
var app = builder.Build();
// Enable serving static files
app.UseStaticFiles(); // works with the web root path
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath , "myWebRoot"))
}); // works with myWebRoot
app.MapGet("/", async (context) =>
{
await context.Response.WriteAsync("Hello");
});
app.Run();
Thank you for any help or insights!

