Skip to content

Static File Handling Issue in .NET 9 Blazor Server Template (MapStaticAssets) #59123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
TwoChickensInFrance opened this issue Nov 23, 2024 · 2 comments
Closed
1 task done
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved

Comments

@TwoChickensInFrance
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Description
When creating a new Blazor Server project using the .NET 9 framework, static file handling via the MapStaticAssets middleware does not work as expected for simple scenarios. Specifically, audio files placed in the wwwroot directory are not served correctly when referenced in the project.

Expected Behavior

Expected Behavior
The audio file (audio1.mp3) should be served correctly from the wwwroot/audioFiles directory, and the audio player should load and play the file without errors.

Actual Behavior
The audio file is not served, and the audio player fails to load the file. There are no errors in the browser's developer tools or console logs. Directly navigating to the file URL (e.g., https://localhost:5001/audioFiles/audio1.mp3) also fails.

Workaround
Replacing app.MapStaticAssets() with app.UseStaticFiles() in Program.cs resolves the issue. Updated Program.cs:

csharp
Copier le code
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles(); // Replaced MapStaticAssets
app.UseAntiforgery();

app.MapRazorComponents()
.AddInteractiveServerRenderMode();

app.Run();

Steps To Reproduce

@page "/"

Home

Hello, world!

Exceptions (if any)

No response

.NET Version

.NET 9.0 (Standard Term Support)

Anything else?

Visual Studio Version: Microsoft Visual Studio Community 2022 (64-bit) - Preview, Version 17.12.0 Preview 5.0
Project Type: Blazor Server

Operating System: [Include OS and version, e.g., Windows 11)

The same project setup works correctly in .NET 8 with app.UseStaticFiles() in Program.cs.
This issue could cause confusion for users transitioning from .NET 8 to .NET 9 or working on simpler projects that rely on static file serving.

@ghost ghost added the area-blazor Includes: Blazor, Razor Components label Nov 23, 2024
@ScarletKuro
Copy link

Please use code blocks when posting code snippets.

You should use:

@Assets["audiofiles/audio1.mp3"]

If you want to access your assets when using MapStaticAssets.
This is documented in the Microsoft Docs: Deliver assets with MapStaticAssets routing endpoint conventions, as well as in the What's New in Blazor in .NET 9: Blazor Static Files Documentation.

This is not an official response, but I believe the answer is correct.

@javiercn
Copy link
Member

javiercn commented Nov 25, 2024

This seems to be a dupe of #58829

We'll use the other issue for tracking purposes.

@javiercn javiercn added the ✔️ Resolution: Duplicate Resolved as a duplicate of another issue label Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved
Projects
None yet
Development

No branches or pull requests

3 participants