0

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:

  1. 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();
  1. 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

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!

3
  • 1
    Hi Mohamed, I use the following code and everything works fine. I have read your code and it seems to be OK. Please create a new sample project and use your existing code to rule out cache issues. Commented Aug 5, 2024 at 13:06
  • 1
    The only difference is that I used an absolute path and yours used a relative path. Commented Aug 5, 2024 at 13:52
  • It wasn’t a cache issue; the problem was with the project configuration. My project was still referencing the wwwroot folder even though I had reconfigured it to use a different directory. To resolve this, I created a new project without including wwwroot, as I had configured it to use another folder. This approach worked fine. Commented Aug 5, 2024 at 16:20

1 Answer 1

0

I am using below code, it works for me.

Program.cs

using Microsoft.Extensions.FileProviders;
// change the root path like below
string basedir = AppDomain.CurrentDomain.BaseDirectory;

WebApplicationOptions options = new()
{
    ContentRootPath = basedir,
    Args = args,
    WebRootPath = Path.Combine(basedir, "myRoot")
};

var builder = WebApplication.CreateBuilder(options);

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();


app.UseStaticFiles(new StaticFileOptions()
{
    FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "myWebRoot")),
    RequestPath = "/myWebRoot" 
});


app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

It wasn’t a cache issue; the problem was with the project configuration. My project was still referencing the wwwroot folder even though I had reconfigured it to use a different directory. To resolve this, I created a new project without including wwwroot, as I had configured it to use another folder. This approach worked fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.