When ASPNETCORE_ENVIRONMENT changed to anything but "Development" - Blazor app doesn't see resources from razor class libraries when debugged in VS

Viewed 818

Maybe just try the example:

https://github.com/HTD/BlazorProfiles

This is a demo Blazor application (default). I created 2 custom application profiles: Development and Production. Both should be debuggable just with pressing F5 key in Visual Studio.

Then I created a dummy Razor Component Library and named it RCL. I made it a dependency for the main project. Inside my dummy RCL, within wwwroot directory I added extra.css file. The role of that file is to change the background color to light green. If it happens - it means the file has been loaded. If the background remains gray / white, it is not loaded.

Here's the _Host.cshtml fragment loading the CSS:

<link href="_content/RCL/extra.css" rel="stylesheet" />

Try to run it. It works. However on one condition: ASPNETCORE_ENVIRONMENT environment variable MUST be set to "Development".

If you test the Production profile (or change the environment variable to "Production") and then run the application - the extra.css file will not be loaded.

enter image description here

The background doesn't change to green, so something is wrong. Why does it happen? Is it a bug in Visual Studio?

If you wonder why do I need to set this environment variable: it allows loading of additional

appsettings.[ASPNETCORE_ENVIRONMENT].json

file. So I can have different configurations for my app. I need to test my app with different configurations. Now I can only debug it in Visual Studio with "Development" configuration. When I change configuration to anything else I can't run my app because it can't load necessary JavaScript files required by components from the library.

So, expected behavior: no matter what I put into "ASPNETCORE_ENVIRONMENT" - the files from RCL should always load. Why shouldn't they?

Actual behavior: using different configurations (profiles) breaks Razor Component Library loading.

2 Answers

Found it in the ASP.NET core github issues. It's actually mentioned in the docs.

You need to enable static web assets in the webhost:

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder => {
                webBuilder.UseStaticWebAssets(); // <----
                webBuilder.UseStartup<Startup>();
            });

edit: note the comment in the Docs

Calling UseStaticWebAssets isn't required when running an app from published output (dotnet publish).

This had me baffled for a few hours today til I came across the accepted answer, which led me to the docs. For a CORE 6.0 app things have changed slightly:

...static web assets are enabled by default in the Development environment. To support assets in other environments when running from build output, call UseStaticWebAssets on the host builder in Program.cs:

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

You then also need to call

app.UseStaticFiles();

further down after the app has been built from var app = builder.Build();

Related