appSettings.json in .Net Core - ProgramIdea

appSettings.json in .Net Core

Get key value

You can get value of any key

{
  "Version": "1.0.0",
  "WebUrl": "http://programidea.com/"  
}
public void Configure(IConfiguration config)
{
    string version = config["Version"];
    string webUrl = config["WebUrl"];
}

Get hierarchical key value

You can get the hierarchical data key by flatteming the data with use of delimiter in the configuration keys.

You can falttene the keys by use of colon(:) to make unique key path. For example "Logging:LogLevel:Default"

{  
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}
public void Configure(IConfiguration config)
{
    var logLevel1 = config["Logging:LogLevel:Default"];
    var logLevel2 = config["Logging:LogLevel:Microsoft"];
    var logLevel3 = config["Logging:LogLevel:Microsoft.Hosting.Lifetime"];
}

GetValue

You can use GetValue method and it's overloaded methods to get value of a specific key from appSettings.json file and convert the value into declare type.

Advantage of overloading method is that if any specific key is not present then i will take default defined value.

{
  "sessionTimeOut": "60"
}
public void Configure(IConfiguration config)
{            
    // GetValue() method
    // Get key value and convert into specific type
    var timeOut1 = config.GetValue(typeof(int), "sessionTimeOut");
    var timeOut2 = config.GetValue<int>("sessionTimeOut");
    // Above both methods are same

    // Get default declared value 20 if key is not present
    var timeOut3 = config.GetValue(typeof(int), "session123", 20);
    var timeOut4 = config.GetValue<int>("sessionXYZ", 20);
    var timeOut5 = config.GetValue("sessionABC", 20);
    // Above three methods are same
}

GetSection

You can use GetSection method to get a part of key details from appSettings.json file.

It will return only key and path, it will not return the value of key.

To get value of key from section either you can you full path separated by (:) or GetChildren method.

{  
  "Admin": {
    "Name": "ProgramIdea",
    "Id": "P001"
  }
}
public void Configure(IConfiguration config)
{
    var adminSection = config.GetSection("Admin");
}

GetChildren

You can use GetChildren method to get the value of any section that returned from GetSection method.

public void Configure(IConfiguration config)
{
    var logLevel = config.GetSection("Admin").GetChildren();
}

Exists

You can you Exists method to check any specific key is present or not. It's return type is boolean.

public void Configure(IConfiguration config)
{   
    bool isUsersExists = config.GetSection("Employee").Exists();
}

Get Array list

You can also get the list of data from appSettings.json file.

{  
  "Users": [
    {
      "Name": "ProgramIdea",
      "Role": "Administrator"
    },
    {
      "Name": "Piyush",
      "Role": "Manager"
    },
    {
      "Name": "Ravi",
      "Role": "Employee"
    }
  ]
}
public void Configure(IConfiguration config)
{
    // Read the user list
    var users = config.GetSection("Users").Get<List<User>>();
}

class User
{
    public string Name { get; set; }
    public string Role { get; set; }
}

Demo:

appSettings.json

{
  "Version": "1.0.0",
  "WebUrl": "http://programidea.com/",
  "sessionTimeOut": "60",
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "Admin": {
    "Name": "ProgramIdea",
    "Id": "P001"
  },
  "Users": [
    {
      "Name": "ProgramIdea",
      "Role": "Administrator"
    },
    {
      "Name": "Piyush",
      "Role": "Manager"
    },
    {
      "Name": "Ravi",
      "Role": "Employee"
    }
  ]
}
demo