I have a.Net Core project that I am trying to run from VS. I am trying to access values from appsettings.json
Let's say I have this value in appsettings.json
"Heading":
{
"Path": "url"
}
And I am trying to get that value in Startup.cs
public void ConfigureServices(IServiceCollection services)
{
string path = Configuration["Heading:Path"];
}
This comes up null. Am I not accessing it properly?
The Program.cs has the following...
var config = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange:
true)
.AddJsonFile($"appsettings.{environmentName}.json", optional:
true, reloadOnChange: true)
.Build();
Your code seems to be fine, I just tried it myself and both path variables are set to "url".
private readonly IConfiguration _configuration;
public Startup(IConfiguration configuration)
{
_configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
string path = _configuration.GetSection("Heading:Path").Value;
string path2 = _configuration["Heading:Path"];
}
The appsettings.json should look like this (maybe missing the outer brackets?):
{
"Heading": {
"Path": "url"
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.