Search code examples
blazorblazor-client-sideasp.net-blazorblazor-webassembly

How to access attributes from launchSettings.json file in Razor Pages (Blazor WebAssembly)?


        "APIs": {
        "API-1": "http://localhost:5000/student",
        "API-2":  "http://localhost:5001/teacher"}

I created these attributes in launchSettings.json file. Now I need to access API-1 and API-2 values in Student.razor page. I tried to use it like this..

List<Student> students = await http.GetFromJsonAsync<List<Student>>("API-1");
    

Solution

  • You don't use launchsettings for that, you should use appsettings.json

    Create an appsettings.json in wwwroot and put your api config in there.

    {
      "APIs": {
        "API-1": "http://localhost:5000/student",
        "API-2": "http://localhost:5001/teacher"
      }
    }
    

    Then inject IConfiguration wherever you need it. e.g.

    @inject Microsoft.Extensions.Configuration.IConfiguration config
    

    and

    List<Student> students = await http.GetFromJsonAsync<List<Student>>(config["APIs:API-1"]);