8

ASP Core 5.0 and VS 2019 Preview 16.9 have CSS scopes feature similar to popular JS frameworks, like Angular. After creating new project, Host.html contains auto-generated CSS.

<link rel="stylesheet" href="MyNameSpace.styles.css" />
  • When environment is set to Development, everything works fine
  • Then, I create appsettings.Live.json and set environment to Live, CSS is not generated and HTTP request trying to load this CSS shows 404 Not Found

What am I missing?

Anonymous
  • 1,823
  • 2
  • 35
  • 74
  • 1
    I assume you mean `_Host.cshtml`? I created a Blazor server app for .NET 5 called `Blazor50server`, set up a live environment, changing the `launchSettings.json` to **live** and had the same result. I know it worked in earlier betas of 5.0 so not sure why this is – Quango Nov 26 '20 at 10:14
  • 1
    Oh, I checked on blazor issues page, but didn't see anything. Have logged this report: https://github.com/dotnet/aspnetcore/issues/28174 – Quango Nov 26 '20 at 10:33

2 Answers2

10

So it appears that the static web assets are only generated in Development mode

I amended the CreateHostBuilder method in Program.cs accordingly:

webBuilder.UseStaticWebAssets().UseStartup<Startup>();

This appears to fix it.

The environments Production and Staging don't seem to need this when the app is published

Quango
  • 12,338
  • 6
  • 48
  • 83
  • It is documented but it's easy to miss: https://learn.microsoft.com/th-th/aspnet/core/razor-pages/ui-class?view=aspnetcore-3.1&tabs=visual-studio#consume-content-from-a-referenced-rcl – Quango Nov 27 '20 at 10:34
4

Adding to @Quango's answer for .NET 6.

Add this line to Program.cs before you build the app:

builder.WebHost.UseStaticWebAssets();
// Add this: ☝
var app = builder.Build();
Ash K
  • 1,802
  • 17
  • 44