An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
Hy @Venkatesh H In C# the common approach is to create a separate class for each configuration section and bind each section individually.
For example, given this appsettings.json:
{
"ConnectionString": {
"Server": "localhost",
"Database": "MyDB",
"User": "sa",
"Password": "password123"
},
"SMTP": {
"Host": "smtp.gmail.com",
"Port": 587,
"Username": "******@gmail.com",
"Password": "smtpPassword"
}
}
Create classes for each section:
public class ConnectionStringSettings
{
public string Server { get; set; }
public string Database { get; set; }
public string User { get; set; }
public string Password { get; set; }
}
public class SmtpSettings
{
public string Host { get; set; }
public int Port { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
Then read each section:
using Microsoft.Extensions.Configuration;
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var connectionSettings = configuration
.GetSection("ConnectionString")
.Get<ConnectionStringSettings>();
var smtpSettings = configuration
.GetSection("SMTP")
.Get<SmtpSettings>();
Alternatively, if you only need a single value:
string server = configuration["ConnectionString:Server"];
string smtpHost = configuration["SMTP:Host"];