1

I am learning about environment variables and how to use them on dev and in production on azure. So far I have some setting values in my appsettings.json file within the values section. I want to get this setting out. I tried

string value = Environment.GetEnvironmentVariable("TestValue");

doesn't return anything.

Here are my settings:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "Values": {
    "TestValue": "HelloWorld",
  },
  "AllowedHosts": "*"
}

I want to set the variable in my appsettings and then call getvariables function to get them is that possible. The reason I want to do this is because I don't want to check in my appsettings.json file on git, instead when I deploy to production, I want to add the settings myself manually on Azure using the variables.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
jo1
  • 115
  • 1
  • 12
  • 1
    Because your json is setting file, not a container for environment variables. – Guru Stron Aug 11 '22 at 00:33
  • 1
    [related question](https://stackoverflow.com/questions/58685686/how-to-set-environment-variables-from-appsettings-json-for-net-core-console-app) – Jonesopolis Aug 11 '22 at 00:34

1 Answers1

0
  • Create a class with name Details and declare variables
  public class Details
    {
        public string Title { get; set; }
        public string Name { get; set; }
    }
  • In Program.cs add the below code
var builder = WebApplication.CreateBuilder(args);
Details myDetails = builder.Configuration.GetSection("Values").Get<Details>();
var app = builder.Build();

Output:

enter image description here

Harshitha
  • 3,784
  • 2
  • 4
  • 9