In .NET framework i usually store key value pipeline variables in a app.config file and access those key values by doing the following
<configuration> <appSettings> <add key="BaseURL" value=""/> <add key="Env" value=""/> <add key="API_KEY" value=""/> </appSettings> </configuration> public static class PipelineVariables { public static string BaseURL => ConfigurationManager.AppSettings["BaseURL"]; public static string Environment => ConfigurationManager.AppSettings["Env"]; public static string APIKEY => ConfigurationManager.AppSettings["API_KEY"]; } Just for fun i want to do a API test automation project using Visual Studio Code and .Net Core
I created the following appsettings.json
{ "AppSettings": { "BaseURL": "", "Env": "", "API_KEY": "" } } Just wondering whats the recommended way to access these key values? I will use these values as pipeline variables in which i will store in Azure. I want to access the key values to pass into my code.
***UPDATE: I got it working similar to the way i do it in .NET Framework projects
I would like the be able to use switch statement on these variables for use in Azure for CI/CD, hence why i want separate methods for variable
However if the a way to simplify the below code?
public static string APIKey => new ConfigurationBuilder() .SetBasePath(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName) .AddJsonFile("appsettings.json", true, true).Build().GetSection("AppSettings:API_KEY")?.Value; public static string BaseURL => new ConfigurationBuilder() .SetBasePath(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName) .AddJsonFile("appsettings.json", true, true).Build().GetSection("AppSettings:BaseURL")?.Value;