Ibanez1408
Ibanez1408

Reputation: 5078

Cannot get the value of appsettings.json in Blazor WASM

I have this in my "appsettings.json"

"AllowedHosts": "*",
  "AlprReport": {
    "ConnectionAddress": "http://192.168.100.37:81/alprreport/cashdeclaration"
  }

I tried getting it in my Razor as so:

public RazorComponent : ComponentBase
{
    [Inject] IConfiguration configuration;
    
    public void SomeMethod() 
    {
        var result = configuration.GetSection("AlprReport:ConnectionAddress").Value
    }
}

I always get null value on result. I tried getting it from my "Program.cs" using this:

var alprReport = builder.Configuration.GetSection("AlprReport:ConnectionAddress").Value;

Still I cannot get it to work. What am I doing wrong here?

Upvotes: 3

Views: 3693

Answers (3)

Dimitris Maragkos
Dimitris Maragkos

Reputation: 11392

It looks like you have a blazor wasm application hosted on asp.net core server. In this scenario you actually have two appsettings.json files, one for the server application and one for the client. You are probably trying to access server's appsettings.json from your client but you can't access server files like that. For the client you have to create a separate appsettings.json file located inside wwwroot folder as you can also see in the documentaion: https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/configuration?view=aspnetcore-6.0

If you want to access server's appsettings from client you have to expose it via an api (bad idea if you store sensitive data in configuration). Example:

[ApiController]
[Route("api/[controller]")]
public class InfoController : ControllerBase
{
    private readonly IConfiguration _configuration;

    public InfoController(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    [HttpGet]
    [Route("config")]
    public async Task<IActionResult> GetConfiguration()
    {
        var result = _configuration["AlprReport:ConnectionAddress"];
  
        return Ok(result);
    }
}

Then in Program.cs, modify the existing HttpClient service registration to use the client to read the file:

var http = new HttpClient()
{
    BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)
};

builder.Services.AddScoped(sp => http);

using var response = await http.GetAsync("api/info/config");
using var stream = await response.Content.ReadAsStreamAsync();

builder.Configuration.AddJsonStream(stream);

Blazor App settings configuration

Upvotes: 2

Raffy
Raffy

Reputation: 605

Check the path where your appsettings.json is located. Since it's Blazor WASM, it should be inside the wwwroot folder.

Upvotes: 7

Nb777
Nb777

Reputation: 2032

Try using GetConnectionString:

    "ConnectionStrings": {
    "ConnectionAddress": "http://192.168.100.37:81/alprreport/cashdeclaration"
  }
  // then the same code with injection but use instead `GetConnectionString`
  var result = configuration.GetConnectionString("ConnectionAddress");

Or continue your way and change your code to :

configuration.GetSection("AlprReport")["ConnectionAddress"]

because GetSection is a key value method!

Upvotes: 1

Related Questions