How to place appsettings.json in RCL project to be consumed by Configuration of blazor wasm app

Charles Kim 5 Reputation points
2024-11-02T10:19:31.8366667+00:00

I have a strategy to share App.razor from RCL project with Blazor hosting apps like Blazor Webassembly standalone app as a front-end app and Blazor hybrid apps as native apps.

I placed all assets into a RCL project(RCL) including appsettings.json file in RCL/wwwroot/ and configured it as Content and Copy.

Building RCL project moved it in RCL/bin/.../wwwroot/appsettings.json.

I added Blazor Webassembly standalone app(Wasm project) into the solution, set up project reference to RCL, removed its own appsettings.json and all razor files, and changed Wasm.Program.cs as below:

builder.RootComponents.Add<RCL.App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

After bulding the solution, I have also checked RCL's appsettings.json is placed in Wasm/bin/../wwwroot/.

However, the problem was the file was not included in the Configuration.

So, I tried adding this line in Wasm.Program but it wouldn't work.

builder.Configuration.AddJsonFile("_content/RCL/appsettings.json");

// or
// builder.Configuration.AddJsonFile("/appsettings.json");

tried also this in Wasm/wwwroot/index.html to see if the file is downloaded to browser cache but to fail.

<head>
<link rel="import" href="_content/RCL/appsettings.json"/>

How can I achieve my goal?

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,612 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 67,921 Reputation points
    2024-11-03T17:11:41.8766667+00:00

    RCL projects do not support web assets by default. You need to add support. See

    https://devblogs.microsoft.com/dotnet/build-client-web-assets-for-your-razor-class-library/

    note: it not clear why the RCL would have its own app settings file. It should use defaults, and allow overriding via published settings.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.