'How to call .UseStaticWebAssets() on WebApplicationBuilder?

I have a problem similar to the one outlined in this question.

When using custom environments while running locally, some files are not being loading (resulting in 404s, etc).

Question: How can I call .UseStaticWebAssets() using the WebApplicationBuilder?

In my program.cs, I tried adding some code to call .UseStaticWebAssets() like below, but I'm seeing the error ConfigureWebHost() is not supported by WebApplicationBuilder.Host. Use the WebApplication returned by WebApplicationBuilder.Build() instead.

// program.cs
public static void Main(string[] args)
{
    var builder = WebApplication.CreateBuilder(args);
    builder.Host.ConfigureWebHostDefaults(builder => 
    {
        builder.UseStaticWebAssets();
    });

    // etc...
}

If I try using the WebHost prop on the aforementioned builder, I see compiler errors when trying to call the UseStaticWebAssets() method.



Solution 1:[1]

I faced the same issue, and found a workaround here. Basically this gets around the exception; seems like this will be fixed in .NET 7.

builder.WebHost.UseWebRoot("wwwroot").UseStaticWebAssets();

Good luck!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 domino