如何从ASP.NET Core 3.1中的appsettings.json中读取整个部分?

问题描述 投票:1回答:1

我想从appsettings.json获取整个部分。

这是我的appsettings.json:

{
 "AppSettings": {
  "LogsPath": "~/Logs",
  "SecondPath": "~/SecondLogs"
  } 
}

C#:

var builder = new ConfigurationBuilder()
           .SetBasePath(Directory.GetCurrentDirectory())
           .AddJsonFile(this.SettingsFilesName);
        configuration = builder.Build();

此语法可以正常工作,并返回“〜/ Logs”:

configuration.GetSection("AppSettings:LogsPath");

但是我如何拥有所有的“ AppSettings”部分?有可能吗?

此语法无效,并且value属性为null。

 configuration.GetSection("AppSettings");

UPDATE

我没有模型,只能在课堂上阅读。我正在寻找这样的东西:

 var all= configuration.GetSection("AppSettings");

并像]一样使用它>

all["LogsPath"] or  all["SecondPath"]

他们将它们的价值还给我。

我想从appsettings.json获取整个部分。这是我的appsettings.json:{“ AppSettings”:{“ LogsPath”:“〜/ Logs”,“ SecondPath”:“〜/ SecondLogs”}} C#:var builder = new ...

c# asp.net-core .net-core appsettings
1个回答
1
投票
是设计使然
© www.soinside.com 2019 - 2024. All rights reserved.