using Microsoft.Extensions.FileProviders; var builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorPages(); builder.Services.AddControllersWithViews(); builder.Services.AddDirectoryBrowser(); var app = builder.Build(); if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseFileServer(new FileServerOptions { FileProvider = new PhysicalFileProvider( Path.Combine(builder.Environment.ContentRootPath, "MyStaticFiles")), RequestPath = "/StaticFiles", EnableDirectoryBrowsing = true }); app.UseAuthorization(); app.MapDefaultControllerRoute(); app.MapRazorPages(); app.Run();
https://github.com/huzuohuyou/DotNet6-Examples/tree/main/UseStaticFiles