Reputation: 1762
The Startup class contains
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
Console.WriteLine($"{env.EnvironmentName.ToString()}");
if (env.IsDevelopment())
{
// For more details on using the user secret store see
// https://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
but env.EnvironmentName.ToString() returns "Production".
I already setup my ASPNETCORE_ENVIRONMENT to "Development" in launchSettings.json
Upvotes: 8
Views: 5161
Reputation: 1350
This might be helpful to some.
I was running locally against published files - that is not being run through visual studio F5. Setting the environment variable through the command line added it to the user environment variables which did not work.
What I had to do to get it to work was to add ASPNETCORE_ENVIRONMENT to the system environment variables. Then, after a reboot, it worked.
To get to environment variables: Right-click on "This PC", then properties. Then Advanced system settings. Then finally click on button "environment variables". In the next pop up add to system variables.
Upvotes: 2
Reputation: 1762
Got this working using powershell command:
$Env:ASPNETCORE_ENVIRONMENT = "Development"
Upvotes: 1
Reputation: 20047
This usually happens when you have setup environment in web.config
too.
For example, if you have environment setup as Production
in launchSettings.json
-
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
},
And in web.config
, if you have other environment Staging
-
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Staging" />
</environmentVariables>
</aspNetCore>
In this case, you will get Staging
when you are trying to read env.EnvironmentName
in startup.cs
See if this helps.
Upvotes: 6