【发布时间】:2021-05-29 06:05:14
【问题描述】:
我需要从 asp.net 核心的 configuration.json 文件中获取数组。 我创建了一个从配置文件中获取部分的 API,但我什么也没得到。 请帮我找出答案! 这是configuration.json文件。
"Settings": {
"Standards": [ "LKG", "UKG", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "COL" ],
"PaymentModes": [ "CASH", "DD", "TRANSFER", "RTGS", "NEFT", "CC", "NB" ],
"FeeTypes": [ "SCHOOL FEE", "HOSTEL FEE", "TRANSPORTATION FEE", "WELCOME PACK FEE", "TOUR FEE", "COACHING FEE", "OTHER FEE" ],
"Installments": [ "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X" ],
"FinancialInstitutes": ["FEDERAL","ICICI","PAYU","AXIS","AXISRTGS","PAYTM"]
}
这里是 api。
[HttpGet]
[AllowAnonymous]
[Route("Settings")]
public List<string> GetSettings()
{
var settings = Configuration.GetSection("Settings").Get<List<string>>();
return settings;
}
这里是启动类。
public class Startup
{
private readonly string _contentRootPath;
public IWebHostEnvironment env;
public IConfiguration Configuration { get; }
public Startup(IWebHostEnvironment env)
{
_contentRootPath = env.ContentRootPath;
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("configuration.json", optional: true, reloadOnChange: true)
.AddJsonFile($"configuration.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
this.Configuration = builder.Build();
this.env = env;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IConfigurationRoot>(provider => { return (IConfigurationRoot)Configuration; }); services.AddMvc()
.AddControllersAsServices();
services.AddHttpContextAccessor();
}
}
【问题讨论】:
-
您正在尝试获取一维数组。但是在配置 json 中你有二维数组。尝试将 List
替换为 string[][] 或 List - >
标签: c# asp.net-core .net-core configuration json.net