Home > OS >  How to read environment variables in .NET 6?
How to read environment variables in .NET 6?

Time:11-20

in my .NET Core 3.1 WebApi project I'm reading environment variable as the very first thing and loading appsettings.json according to it:

public static IHostBuilder CreateHostBuilder(string[] args)
{
  string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
  ...
}

But I didn't find out how to read it in .NET 6:

var builder = WebApplication.CreateBuilder(args);

build.Environment has no way to read it.
Anyone knows?

Thanks

CodePudding user response:

The official documentation says that the method in the System namespace should work:

https://docs.microsoft.com/en-us/dotnet/api/system.environment.getenvironmentvariable?view=net-6.0

  • Related