appsettings.json is not loaded .net 8 blazor web assembly

116 views Asked by At

This line causes a null reference exception:

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.Configuration.GetValue<string>("BaseApiUrl")) });

This is my appsettings.json that I put in the wwwroot folder directly:

{
  "BaseApiUrl": "https://localhost:44327"
}

Do you know what the problem is?

I expect to have the correct value returned.

1

There are 1 answers

0
Iman Sharifi On BEST ANSWER

You can use AddHttpClient extention

builder.Services.AddHttpClient("BaseHttp", httpClient =>
{
    httpClient.BaseAddress = new Uri(new Uri(builder.Configuration.GetValue<string>("BaseApiUrl")));
});