aspnetcore: Unable to call StaticWebAssetsLoader.UseStaticWebAssets after upgrading to VS 2022 official release

Have a Blazor Server .NET 6 application where my local development environment uses the name “Local” instead of the default “Development”. Out-of-the-box, the application is returning 404 for the static content like CSS files. Admittedly, I am having problems finding the official suggestions at this time but the suggestions out there state that you need to manually call StaticWebAssetsLoader.UseStaticWebAssets() for the environments other than Development.

var builder = WebApplication.CreateBuilder(args);

builder.WebHost.ConfigureAppConfiguration((ctx, cb) => {
	// https://github.com/dotnet/aspnetcore/blob/main/src/DefaultBuilder/src/WebHost.cs#L219
	if (ctx.HostingEnvironment.IsEnvironment("Local")) {
		// This call inserts "StaticWebAssetsFileProvider" into the static file middleware
		StaticWebAssetsLoader.UseStaticWebAssets(ctx.HostingEnvironment, ctx.Configuration);
	}
});

This has been working with both ASP.NET 5 and (up until yesterday) with ASP.NET 6. Once I upgraded from latest Visual Studio 2022 Preview to Visual Studio 2022 Current build, I am getting the following exception on application startup.

Unhandled exception. System.NotSupportedException: The web root changed from "C:\Development\Repos\Staging\ABC\ABC.UI\wwwroot" to "C:\Development\Repos\Staging\ABC\ABC.UI\". Changing the host configuration using WebApplicationBuilder.WebHost is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.
   at Microsoft.AspNetCore.Builder.ConfigureWebHostBuilder.ConfigureAppConfiguration(Action`2 configureDelegate)
   at Program.<Main>$(String[] args) in C:\Development\Repos\Staging\ABC\ABC.UI\Program.cs:line 25

C:\Development\Repos\Staging\ABC\ABC.UI\bin\Debug\net6.0\ABC.UI.exe (process 27360) exited with code -532462766.

The exception is not being caused by the call to StaticWebAssetsLoader.UseStaticWebAssets(), it seems to be raised due to the outer call to WebHost.ConfigureAppConfiguration(). I understand that the new recommended way of changing options is to create a new instance of WebApplicationOptions and pass that in.

var opt = new WebApplicationOptions() {
	Args = args
};

var builder = WebApplication.CreateBuilder(opt);

But in this case, I am not trying to set any of those options. In essence, how do I call StaticWebAssetsLoader.UseStaticWebAssets() from outside ConfigureAppConfiguration while passing in the appropriate context and configuration as arguments?

Using a global.json file I was able to confirm that this approach works with 6.0.100-rc.2.21505.57 but is now broken with 6.0.100.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 21 (12 by maintainers)

Most upvoted comments

Had the same issue with VS 17.1.3, and this resolved it:

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseWebRoot("wwwroot").UseStaticWebAssets();

Documented in the answers here: https://stackoverflow.com/questions/64833632/blazor-css-isolation-not-working-and-not-adding-scope-identifiers-after-migratin

Found these issues on the same subject: https://github.com/dotnet/aspnetcore/issues/28911 https://github.com/dotnet/aspnetcore/issues/28174

#28174, references this article, which probably needs updating for an ASP.NET 6.0 based on minimal-hosting model: https://docs.microsoft.com/th-th/aspnet/core/razor-pages/ui-class?view=aspnetcore-6.0&tabs=visual-studio#consume-content-from-a-referenced-rcl

@captainsafia works absolutely fine with 7.0-preview3:

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseStaticWebAssets();

I’m fine with this workaround for 6.0:

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseWebRoot("wwwroot").UseStaticWebAssets();

The info in this issue should help others find this workaround. A backport to the next 6.0 servicing might be nice to save others time from bumping into this. It was sort of unexpected, something as mundane as creating a launchSettings.json entry for a production profile-- and suddenly an exception you didn’t receive under the development profile.

Appreciate all the help and understanding!

I seem to have found a solution to this issue but would appreciate feedback on this approach.

Instead of calling this

builder.WebHost.ConfigureAppConfiguration((ctx, cb) => {
	// https://github.com/dotnet/aspnetcore/blob/main/src/DefaultBuilder/src/WebHost.cs#L219
	if (ctx.HostingEnvironment.IsEnvironment("Local")) {
		// This call inserts "StaticWebAssetsFileProvider" into the static file middleware
		StaticWebAssetsLoader.UseStaticWebAssets(ctx.HostingEnvironment, ctx.Configuration);
	}
});

I now have

if (builder.Environment.IsEnvironment("Local")) {
	// Was thrown off since I did not realize that ConfigurationManager implemented IConfiguration and that builder.Configuration can be passed in
	StaticWebAssetsLoader.UseStaticWebAssets(builder.Environment, builder.Configuration);
}

Thoughts?

What’s wrong with just the following @CodeFontana?

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseStaticWebAssets();