Asp.net core applications take from the launchsettings.json file when developing in visual studio and when you publish the app will look at the web.config or app.config file. So keep this in mind if you are using visual studio. I'll show you how to set that up aswell. (NOTE: I'm using asp.net core MVC which might be different)
Your launchsettings.json file is located under properties:

And should look something like this
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:58251",
"sslPort": 44362
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Workorders2": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
When you publish, the app will use your app.config settings. Set it up like this:
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT")" value="Production" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</configuration>
Then all you need to do is get the environment variable like this:
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")");