Fix dashboard static files loading when working directory differs from executable location#12902
Fix dashboard static files loading when working directory differs from executable location#12902aaron-seq wants to merge 4 commits intomicrosoft:mainfrom
Conversation
…m executable location Resolves microsoft#1979 The dashboard fails to render static content when run from a different working directory than the executable location. This occurs because UseStaticFiles() defaults to using WebRootFileProvider which resolves paths relative to the current working directory. This fix explicitly configures a PhysicalFileProvider pointing to the wwwroot directory relative to AppContext.BaseDirectory (the executable's location), ensuring static assets load correctly regardless of where the process is launched from.
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 12902Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 12902" |
|
@aaron-seq please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement ( “Agreement” ) is agreed to by the party signing below ( “You” ), 1. Definitions. “Code” means the computer software code, whether in human-readable or machine-executable form, “Project” means any of the projects owned or managed by .NET Foundation and offered under a license “Submit” is the act of uploading, submitting, transmitting, or distributing code or other content to any “Submission” means the Code and any other copyrightable material Submitted by You, including any 2. Your Submission. You must agree to the terms of this Agreement before making a Submission to any 3. Originality of Work. You represent that each of Your Submissions is entirely Your 4. Your Employer. References to “employer” in this Agreement include Your employer or anyone else 5. Licenses. a. Copyright License. You grant .NET Foundation, and those who receive the Submission directly b. Patent License. You grant .NET Foundation, and those who receive the Submission directly or c. Other Rights Reserved. Each party reserves all rights not expressly granted in this Agreement. 6. Representations and Warranties. You represent that You are legally entitled to grant the above 7. Notice to .NET Foundation. You agree to notify .NET Foundation in writing of any facts or 8. Information about Submissions. You agree that contributions to Projects and information about 9. Governing Law/Jurisdiction. This Agreement is governed by the laws of the State of Washington, and 10. Entire Agreement/Assignment. This Agreement is the entire agreement between the parties, and .NET Foundation dedicates this Contribution License Agreement to the public domain according to the Creative Commons CC0 1. |
|
@davidfowl The PR is ready for review. |
|
Thanks for the PR! The fix looks good - using AppContext.BaseDirectory is the right approach. However there are duplicate using statements in the diff and CI is failing. Please rebase on main and remove the duplicates, then this should be good to merge! |
|
Thanks @davidfowl for the quick review! You're absolutely right about the duplicate using statements. The merge conflict introduced duplicates for:
I'll rebase on main and clean up the duplicates immediately. The actual fix (FileProvider configuration) remains correct. Will push the cleaned up version shortly. Summary of corrected changes:
Rebasing now. |
Removed duplicate using statements that were causing CI build failures: - using System.Diagnostics; (line 6) - using System.Diagnostics.CodeAnalysis; (line 7) - using System.Reflection; (line 9) The FileProvider configuration remains correct.
|
CI is failing - |
davidfowl
left a comment
There was a problem hiding this comment.
E2E tested - verified the bug fix works correctly. Static files now load properly when dashboard is run from a different working directory than the executable location. Core scenario also verified working.
|
E2E tested and verified the fix works - static files load correctly when running dashboard from a different working directory. The failing CI checks are unrelated flaky WaitForTests in Hosting integration tests. cc @JamesNK |
|
Originally when I filed the bug the, I ran the binary from a different folder, not the source. I think it is broken during dev because at dev time, the content isn't published so static files. So for dotnet run it needs to use cwd (or at least the project directory). |
|
To support both scenarios, we need a fallback mechanism: var baseDirectory = AppContext.BaseDirectory;
var wwwrootFromBase = Path.Combine(baseDirectory, "wwwroot");
// Probe for wwwroot location - prefer executable directory, fallback to current directory
string wwwrootPath;
if (Directory.Exists(wwwrootFromBase))
{
wwwrootPath = wwwrootFromBase;
}
else
{
var cwdWwwroot = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
wwwrootPath = Directory.Exists(cwdWwwroot) ? cwdWwwroot : wwwrootFromBase;
}
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(wwwrootPath),
OnPrepareResponse = context =>
{
// existing no-cache logic...
}
});This ensures:
I can help implement this if you'd like, or feel free to push the update yourself! @davidfowl @JamesNK |
|
@aaron-seq yes that looks good. |
|
@davidfowl does this still apply w the bundle stuff? can we merge? |
|
@aaron-seq going to close this as @JamesNK is working on a new command that will make this moot #15607 |

Description
Fixes #1979
The dashboard fails to render static content when run from a different working directory than the executable location.
Root Cause
UseStaticFiles()defaults to usingWebRootFileProviderwhich resolves paths relative toDirectory.GetCurrentDirectory()(the process working directory) rather than the executable location.Solution
Explicitly configure
FileProviderinStaticFileOptionsto usePhysicalFileProviderwithAppContext.BaseDirectoryas the base path. This ensures static files are always located relative to the executable, making the dashboard resilient to where it's launched from.Changes
using System.IO;to importsusing Microsoft.Extensions.FileProviders;to importsFileProvider = new PhysicalFileProvider(Path.Combine(AppContext.BaseDirectory, "wwwroot"))inUseStaticFiles()callTesting