ASP.NET Core specified environment publishing (hosting environment)
ASP.NET Core application publish command:
dotnet publish [<PROJECT>] [-f|--framework] [-r|--runtime] [-o|--output] [-c|--configuration] [--version-suffix] [-v|--verbosity] [-h|--help]
Issue an example command (generated under the bin/release/netcoreapp1.1/publish
directory):
dotnet publish -c release
The above command does not specify EnvironmentName
the release, what does it mean? For example, the configuration in the ASP.NET Core application, the appsettings.json
configuration of the test environment and the production environment are different (such as the database connection string). If the above command is used, we also need to manually copy the appsettings.json
files in different environments, which will be changed later. , and it needs to be updated again, which is very troublesome.
How to solve the above problem is very simple. Specify the environment variable of the development machine or server. ASPNETCORE_ENVIRONMENT
After setting the environment variable, when the dotnet *.dll
startup program is executed, ASP.NET Core will automatically load the appsettings.*.json
file corresponding to this environment variable, for example appsettings.Production.json
.
In fact, when we use VS 2017 F5 to debug the project, the environment variables are also set by default , such as the example configuration ASPNETCORE_ENVIRONMENT
in the ASP.NET Core application :launchSettings.json
"profiles": {
"IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "launchUrl": "api/values", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "AspNetCore.Samples": { "commandName": "Project", "launchBrowser": true, "launchUrl": "api/values", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, "applicationUrl": "http://localhost:59522" } }
Startup
Example configuration:
public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); }
Because in the above configuration , we ASPNETCORE_ENVIRONMENT
are Development
using VS 2017 F5 to debug the project, and the appsettings.Development.json
configuration file under the project will be loaded and used. If this file does not exist, ASP.NET Core will use the appsettings.json
configuration file by default.
So how do we set ASPNETCORE_ENVIRONMENT
environment variables on the server? It's as simple as typing a command.
1. Windows Server Setup
Command Line:
>setx ASPNETCORE_ENVIRONMENT "Development"
SUCCESS: Specified value was saved.
or (requires admin rights)
>setx ASPNETCORE_ENVIRONMENT "Development" /M
SUCCESS: Specified value was saved.
PowerShell
Order:
$Env:ASPNETCORE_ENVIRONMENT = "Prodction"
After Windows sets the environment command, you need to re-open a command line dotnet *.dll
startup item to be effective.
2. MacOS/Linux server setup
Command Line:
export ASPNETCORE_ENVIRONMENT=development
dotnet *.dll
When starting the project, we can see the current one Hosting environment
to check if it is correct, example:
> dotnet AspNetCore.Samples.dll
Hosting environment: Prodtction
Content root path: C:\Users\yuezh\Desktop\Demo\AspNetCore.Samples
Now listening on: http://*:5003 Application started. Press Ctrl+C to shut down.
References:
ASP.NET Core application publish command:
dotnet publish [<PROJECT>] [-f|--framework] [-r|--runtime] [-o|--output] [-c|--configuration] [--version-suffix] [-v|--verbosity] [-h|--help]
Issue an example command (generated under the bin/release/netcoreapp1.1/publish
directory):
dotnet publish -c release
The above command does not specify EnvironmentName
the release, what does it mean? For example, the configuration in the ASP.NET Core application, the appsettings.json
configuration of the test environment and the production environment are different (such as the database connection string). If the above command is used, we also need to manually copy the appsettings.json
files in different environments, which will be changed later. , and it needs to be updated again, which is very troublesome.
How to solve the above problem is very simple. Specify the environment variable of the development machine or server. ASPNETCORE_ENVIRONMENT
After setting the environment variable, when the dotnet *.dll
startup program is executed, ASP.NET Core will automatically load the appsettings.*.json
file corresponding to this environment variable, for example appsettings.Production.json
.
In fact, when we use VS 2017 F5 to debug the project, the environment variables are also set by default , such as the example configuration ASPNETCORE_ENVIRONMENT
in the ASP.NET Core application :launchSettings.json
"profiles": {
"IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "launchUrl": "api/values", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "AspNetCore.Samples": { "commandName": "Project", "launchBrowser": true, "launchUrl": "api/values", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, "applicationUrl": "http://localhost:59522" } }
Startup
Example configuration:
public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); }
Because in the above configuration , we ASPNETCORE_ENVIRONMENT
are Development
using VS 2017 F5 to debug the project, and the appsettings.Development.json
configuration file under the project will be loaded and used. If this file does not exist, ASP.NET Core will use the appsettings.json
configuration file by default.
So how do we set ASPNETCORE_ENVIRONMENT
environment variables on the server? It's as simple as typing a command.
1. Windows Server Setup
Command Line:
>setx ASPNETCORE_ENVIRONMENT "Development"
SUCCESS: Specified value was saved.
or (requires admin rights)
>setx ASPNETCORE_ENVIRONMENT "Development" /M
SUCCESS: Specified value was saved.
PowerShell
Order:
$Env:ASPNETCORE_ENVIRONMENT = "Prodction"
After Windows sets the environment command, you need to re-open a command line dotnet *.dll
startup item to be effective.
2. MacOS/Linux server setup
Command Line:
export ASPNETCORE_ENVIRONMENT=development
dotnet *.dll
When starting the project, we can see the current one Hosting environment
to check if it is correct, example:
> dotnet AspNetCore.Samples.dll
Hosting environment: Prodtction
Content root path: C:\Users\yuezh\Desktop\Demo\AspNetCore.Samples
Now listening on: http://*:5003 Application started. Press Ctrl+C to shut down.
References: