I’m working on an ASP.NET Core application and trying to use an environmental variable for my database connection string. I’ve set the environmental variable Dbconstring in my system settings on Windows. However, when I run my application in debug mode, the connection string is always null.
Here’s the code snippet where I retrieve the environmental variable:
var connectionstring = Environment.GetEnvironmentVariable("Dbconstring");
I have verified that the environmental variable is set correctly in my system settings:
I navigated to System Properties -> Environment Variables.
Under System Variables, I added a new variable with the name Dbconstring and provided the appropriate connection string as its value.
I restarted my computer to ensure the new environmental variable is recognized by the system.
Despite these steps, the connectionstring variable in my code remains null when I debug the application.
What I Have Tried:
Checked Environment Variable Existence:
- I used the cmd command echo %Dbconstring% to confirm that the variable is set and contains the expected value.
Environment Variable Scope: - I set the environmental variable under both User Variables and System Variables but still faced the same issue.
ASP.NET Core Configuration: - I checked if there were any overrides in the appsettings.json or other configuration files, but there are none that affect the connection string.
Stephen OHAERI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.