首页 > 解决方案 > 从 Angular-CLI 读取 appsettings.json

问题描述

有什么方法可以阅读appsettings.jsonfrom.NET CoreAngular-CLI?我不想使用 API。我想直接访问上面的文件。我的问题是我不知道要使用哪个路径,因为它位于内部serverUrl/Files/。我试过serverUrl/Files/appsettings.json了,但即使 UseStaticFiles() middleware我的Startup.cs.

标签: .netangular

解决方案


据我所知,如果您只使用 angular-cli,您可以从 assets 文件夹中读取文件(.json、images..)(因为当 angular-cli 构建应用程序时,会在 dist 中复制此文件夹)。所以,恐怕你只能从这个文件夹加载文件

如果要从 .NET Core 中读取这些值,可以从 startUp 函数中读取。想象一下你的 appsetting.json 就像

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "AppSettings": {
    "NewProperty": {
      "oneProperty": "value one",
      "twoProperty": "value two"
    }
  }
}

您可以使用

public Startup(IConfiguration configuration, IHostingEnvironment env)
{
     ...

    IConfigurationSection newPropertySection =
        Configuration.GetSection("AppSettings:NewProperty");

    IEnumerable<IConfigurationSection> newPropertySectionMembers = 
          newPropertySection.GetChildren();

    foreach (var item in newPropertySectionMembers)
    {
        ...
    }
    ...
}

注意:我使用 .NET Core 2.1


推荐阅读