public static string GetSetting(string keyName)
{
var archivo = "appsettings.json";
var ambiente = System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
if (!string.IsNullOrEmpty(ambiente))
archivo = $"appsettings.{ambiente}.json";
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(archivo);
Configuration = builder.Build();
return Configuration[keyName].ToString().Trim();
}
public class Program
{
public static IConfigurationRoot Configuration { get; set; }
public static void Main(string[] args = null)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
Console.WriteLine($"option1 = {Configuration["option1"]}");
}
}
No hay comentarios:
Publicar un comentario