【问题标题】:How to access attributes from launchSettings.json file in Razor Pages (Blazor WebAssembly)?如何从 Razor 页面(Blazor WebAssembly)中的 launchSettings.json 文件访问属性?
【发布时间】:2020-10-24 12:33:33
【问题描述】:
        "APIs": {
        "API-1": "http://localhost:5000/student",
        "API-2":  "http://localhost:5001/teacher"}

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

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

【问题讨论】:

  • launchSettings.json 仅用于开发。 prod中没有launchSettings

标签: blazor blazor-client-side asp.net-blazor blazor-webassembly


【解决方案1】:

你不要为此使用启动设置,你应该使用 appsettings.json

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

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

然后inject IConfiguration 在任何你需要的地方。 例如

@inject Microsoft.Extensions.Configuration.IConfiguration config

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

【讨论】:

  • @Shruti,这是正确答案...你为什么不接受它!?
猜你喜欢
  • 1970-01-01
  • 2019-12-23
  • 2021-02-05
  • 2020-06-18
  • 2018-10-31
  • 2020-02-18
  • 2021-09-12
  • 1970-01-01
  • 2019-09-18
相关资源
最近更新 更多