Loading Environment Variables From .env File - gecko-8/devwiki GitHub Wiki

Note: Uses this package - DotNetEnv

  1. Add the DotNetEnv package from Nuget.
  2. Open your Program.cs file.
  3. Add a ConfigureAppConfiguration call to your WebHost.CreateDefaultBuilder(args) call in CreateWebHostBuilder.
    ConfigureAppConfiguration((hostingContext, config) =>
    {
        DotNetEnv.Env.Load("../../.env");
    }).
    
    Note: The file path should be relative to the location of your Program.cs file.
    Note: It's important to do it this way say that Code-First Migrations work.