I am trying to setting up multiple environments in my .NET Core 2.0 application. See my code below.
\"configuratio
You can update your launchsettings.json to include a 'Development' profile and then run:
dotnet run --launch-profile "Development"
For further details on configuration of the launchSettings.json file see Working with multiple environments
Note that the commandName would probably need to be "Project" (I haven't really tried this much). Example launchSettings.json as follows:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:19882/",
"sslPort": 0
}
},
"profiles": {
"Development": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
dotnet run --environment
makes no effect on ASPNETCORE_ENVIRONMENT
environment variable, see this issue.
Here's a detailed instruction on how to switch environments in a multiple ways: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments
For example, you can run it from a Command line (before dotnet run
): set ASPNETCORE_ENVIRONMENT=Development
Finally I have done it...
Let’s look at how I achieved this.
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:40088/",
"sslPort": 0
}
},
"profiles": {
"Development": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Azuredev": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Azuredev"
}
}
}
}
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceRoot}/my.api/bin/Debug/netcoreapp2.0/my.api.dll",
"args": [],
"cwd": "${workspaceRoot}/my.api",
"stopAtEntry": false,
"requireExactSource": false,
"internalConsoleOptions": "openOnSessionStart",
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}"
},
"osx": {
"command": "open"
},
"linux": {
"command": "xdg-open"
}
},
"sourceFileMap": {
"/Views": "${workspaceRoot}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
public IConfigurationRoot Configuration { get; }
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, reloadOnChange: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
this.HostingEnvironment = env;
}
After this all changes, my API is working fine with both the F5 debug option as well as CLI terminal.
To launch the application from the command line, use these keywords:
dotnet run --launch-profile "Development"
OR
dotnet run --launch-profile "Azuredev"
For .NET Core 3.1, it happens that you need to add:
.AddCommandLine(args)
to your ConfigurationBuilder in Program.cs
for
dotnet run --environment "Development"
to work (switch ASP.NET Core environments).