.net - 从 Angular-CLI 读取 appsettings.json
问题描述
有什么方法可以阅读appsettings.json
from.NET Core
吗Angular-CLI
?我不想使用 API。我想直接访问上面的文件。我的问题是我不知道要使用哪个路径,因为它位于内部serverUrl/Files/
。我试过serverUrl/Files/appsettings.json
了,但即使 UseStaticFiles() middleware
我的Startup.cs
.
解决方案
据我所知,如果您只使用 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
推荐阅读
- c++ - C++中的继承,内存问题
- php - 更新多维购物车数组中的值
- python - 在另一个文件中移动代码时,Python 静默失败
- javascript - 使用 aws secret manager 配置 knexfile.js
- c++ - C++ 项目中的性能测试
- java - 带有 addOnListChangedCallback 的 ObservableArrayList 的工作示例?
- asp.net-core - 未调用 .NET Core 中间件
- java - 带有自定义堆栈跟踪的 Mockito.doThrow
- django - 通过数千个选项提高外键字段的加载性能
- angular - ng build 有效,但不适用于 --prod