如何从 Razor Pages (Blazor WebAssembly) 中的 launchSettings.json 文件访问属性?

Shr*_*uti 1 blazor blazor-client-side asp.net-blazor blazor-webassembly

        "APIs": {
        "API-1": "http://localhost:5000/student",
        "API-2":  "http://localhost:5001/teacher"}
Run Code Online (Sandbox Code Playgroud)

我在 launchSettings.json 文件中创建了这些属性。现在我需要访问 Student.razor 页面中的 API-1 和 API-2 值。我尝试像这样使用它..

List<Student> students = await http.GetFromJsonAsync<List<Student>>("API-1");
    
Run Code Online (Sandbox Code Playgroud)

Mis*_*goo 5

您不使用 launchsettings ,您应该使用 appsettings.json

在 wwwroot 中创建一个 appsettings.json 并将您的 api 配置放在那里。

{
  "APIs": {
    "API-1": "http://localhost:5000/student",
    "API-2": "http://localhost:5001/teacher"
  }
}
Run Code Online (Sandbox Code Playgroud)

然后inject IConfiguration无论你需要什么地方。例如

@inject Microsoft.Extensions.Configuration.IConfiguration config
Run Code Online (Sandbox Code Playgroud)

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