How to read configuration value
In .NET CORE there is no Web.config but replaced by a appsettings.json file like this one:
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"Version": "2.145.378.4",
}
Imagine we want to read the Version value and use it in the application.
In the Startup.cs file the default is already addressing the configuration.
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
...
}
In any controller class the constructor gets the configuration as a IConfiguration parameter as an interface.
Likewise you can obtain the Version here.
public class HomeController : Controller
{
private IConfiguration Config;
public HomeController(IConfiguration configuration)
{
Config = configuration;
}
...
public async Task Index()
{
//Read the "Version" value return it on the View
ViewBag.Ver = Config.GetSection("Version").Value;
return View();
}
}