3
votes

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.

1
This is an interesting bug. I did the same with another opensource project github.com/Blazored/Typeahead and the css and js did not load when env was not Development.Umair
Reproduced. I tried to switch from Kestrel to IIS, but no difference. So it's in the middleware routing manager.JHBonarius

1 Answers

5
votes

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).