dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.59k stars 10.06k forks source link

MapStaticAssets doesn't work with WebApplication.CreateSlimBuilder #58986

Open NoahStahl opened 2 weeks ago

NoahStahl commented 2 weeks ago

Describe the bug

In an app built with WebApplication.CreateSlimBuilder() and using the new MapStaticAssets in .NET 9, responses for static files are empty with 200 status code. To reproduce, create a new ASPNET Core project from template, then change WebApplication.CreateBuilder() to WebApplication.CreateSlimBuilder() in Program.cs.

It appears that calling UseStaticWebAssets allows this to work, but this isn't documented as far as I can tell.

// Program.cs
var builder = WebApplication.CreateSlimBuilder(args);
builder.WebHost.UseStaticWebAssets();
var app = builder.Build();
...
app.MapStaticAssets();

Would like to understand what is expected and what the correct setup should be. It seems that if CreateSlimBuilder alone isn't sufficient, there should be some kind of warning visible to developer. At the very least, this aspect should be documented.

Expected Behavior

Either MapStaticAssets truly works as a "drop-in replacelement" of UseWebStaticFiles, or incompatbilities are documented and/or warned to developer in tooling/build.

.NET Version

9.0.100

captainsafia commented 1 week ago

cc: @javiercn

There's a chance that the MapStaticAssets API might take a dependency on some services/middleware that aren't registered when you call CreateSlimBuilder.

javiercn commented 1 week ago

@captainsafia I'm trying to think what that would be.

@NoahStahl are you seeing any error on the command line logs? Is there any exception being thrown?

NoahStahl commented 1 week ago

@javiercn The reproduction is pretty straightforward, are you able to see this behavior?

  1. Create a new project from .NET 9 template: dotnet new mvc
  2. On line 1 of Program.cs, change CreateBuilder to CreateSlimBuilder
  3. Launch debug for http. Observe that all the bootstrap script file requests respond with empty 200.

Image