I'm working on .net core api and need to get values from appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"projectList": "Project",
"projectCatList": "Project Categories",
}
}
}
I want to access these vaues in controller class and another .cs class


Vishal YelveRank #178 · 13.6K reputationPosted Nov 3, 2022, 10:04 AM EDT
public class DemoController : ControllerBase { private readonly IConfiguration _configuration; public DemoController(IConfiguration configuration) { _configuration = configuration; } [HttpGet] public bool GetIsLaunchBrower() { var brower = _configuration.GetValue("IIS Express:launchBrowser"); return brower; } }var envrionment = _configuration.GetValue("IIS Express:environmentVariables:ASPNETCORE_ENVIRONMENT");Refer links
https://www.c-sharpcorner.com/article/reading-values-from-appsettings-json-in-asp-net-core/
Rijwan AnsariRank #82 · 24.2K reputationPosted Nov 3, 2022, 10:07 AM EDT
You can do in controller or class.
First inject IConfiguration in Ctor as shown:
private readonly IConfiguration _iconfiguration; public HomeController(IConfiguration iconfiguration) { _iconfiguration = iconfiguration; }Then use configuration as in any Action as shown:
public IActionResult Index() { // First way string value1 = _iconfiguration.GetSection("Data").GetSection("ConnectionString").Value; // Second way string value2 = _iconfiguration.GetValue ("Data:ConnectionString"); var EmailID = _iconfiguration["EmailID"]; var Websitename = _iconfiguration["Websitename"]; var WebsiteURL = _iconfiguration["WebsiteURL"]; return View(); }You can do the same way in class. However, if it better to use interface and implementation.
https://www.c-sharpcorner.com/article/appsettings-6-ways-to-read-the-config-in-asp-net-core-3-0/
RituRank #3,526 · 412 reputationPosted Nov 4, 2022, 5:51 AM EDT
thanks for ur solution
but when i'm using this approach to my code like below:
public class TeemController : ControllerBase
{
public readonly IConfiguration _iconfiguration;
public TeemController(IConfiguration iconfiguration)
{
_iconfiguration = iconfiguration;
}
[HttpGet("getTime")]
public ActionResult getTime(string r, string p, string e)("DevSetting:pr");
{
string p = _iconfiguration.GetValue
PContract pCon = getP() ;
//.....
}
} // it shows p value successfully.
-----------------------------------------------------------------------
But when i'm using this approach to another class
it throws error null reference
public class MyHelper getP() ("DevSetting:pr");
{
public static IConfiguration _iconfiguration;
public MyHelper(IConfiguration iconfiguration)
{
_iconfiguration = iconfiguration;
}
private static List
{
string p = _iconfiguration.GetSection("DevSetting").GetSection("pr").Value;
string p = _iconfiguration.GetValue
//......
}
Jaydeep PatilRank #205 · 11.7K reputationPosted Nov 5, 2022, 1:27 AM EDT
You can simply inject IConfiguration inside the constructor of the particular class and use that object to get environment variables value form appsettings.json
Jignesh KumarRank #91 · 22.9K reputationPosted Nov 6, 2022, 12:09 AM EDT
Hi Ritu,
You need to inject IConfiguration interface in which ever place you need to use. After that you can get values from appsetting.json file,
Please refer this link which will help you out,
https://www.c-sharpcorner.com/article/reading-values-from-appsettings-json-in-asp-net-core/
https://www.c-sharpcorner.com/article/appsettings-6-ways-to-read-the-config-in-asp-net-core-3-0/