.NET Core 读取 JSON 文件

但是现在一时半会想不起来当时记这个的目的是什么了,也忘了使用场景,好像是要读取配置文件注入进某个地方。先放在这里吧。

在类的构造函数里写

_configuration = new ConfigurationBuilder()
	.SetBasePath(Directory.GetCurrentDirectory())
	.AddJsonFile(appsettings.json, false, false)
	// 如果是其他的就写 .AddJsonFile(“JsonFolder/custom.json”)
	.AddEnvirionmentVariables()
	.Build();

就可以了。

用的时候写

_configuration.GetSection[SomeApi:Url]

就可以获取到。

参考地址