Skip to content

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

@TwoChickensInFrance

Description

@TwoChickensInFrance

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions