13

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?

2
  • 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 Commented Nov 26, 2020 at 10:14
  • 1
    Oh, I checked on blazor issues page, but didn't see anything. Have logged this report: github.com/dotnet/aspnetcore/issues/28174 Commented Nov 26, 2020 at 10:33

3 Answers 3

15

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

Sign up to request clarification or add additional context in comments.

1 Comment

It is documented but it's easy to miss: learn.microsoft.com/th-th/aspnet/core/razor-pages/…
14

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();

2 Comments

This still seems to be a problem in NET 8 blazor as well and this does work. I am wondering though if your code should be wrapped in the IsDevelopment() if statement? If not would it cause problems when you go to production and such?
Thanks, this worked on .NET 9 as well.
0

If you are using CreateSlimBuilder method this is the reason, you have to change it to CreateBuilder

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.