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

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:18 (10 by maintainers)

github_iconTop GitHub Comments

5reactions
CodeFontanacommented, Apr 10, 2022

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

3reactions
CodeFontanacommented, Apr 13, 2022

@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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

6.0.x Milestone
[release/6.0] Move to MacOS 13 ... Unable to call StaticWebAssetsLoader.UseStaticWebAssets after upgrading to VS 2022 official release area-minimal Includes ...
Read more >
asp.net core - AspCore 404 on production enviroment
In my case helped adding StaticWebAssetsLoader.UseStaticWebAssets() to Program.cs in BlazorApp.Server.proj : using Microsoft.AspNetCore.
Read more >
Hosting A Blazor Visual Studio Project Directly In IIS
I had a need to view the Blazor application I was developing in IIS. Furthermore, I wanted this application to work even when...
Read more >
StaticWebAssetsLoader.UseStaticWebAssets Method
Configure the IWebHostEnvironment to use static web assets.
Read more >
NET 6 Core Web App Returns web page not found
I finally went to update my VS through the installer and fixed it. Go to VS Installer and click Modify. Under "ASP.NET and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found