ASP. NET Core Application Publishing command:
dotnet publish [<PROJECT>] [-f|--framework] [-r|--runtime] [-o|--output] [-c|--configuration] [--version-suffix] [-v|--verbosity] [-h|--help]
To publish the sample command (generated in the bin/release/netcoreapp1.1/publish
directory):
dotnet publish -c release
The above command does not specify EnvironmentName
a publication, what does it mean? For example, the configuration in the ASP. NET Core application appsettings.json
, the test environment and production environment configuration is not the same (such as database connection string), if using the above release command, we also need to manually copy the different environment of the appsettings.json
files, later to change, but also need to publish updates, very cumbersome.
How to solve the above problem, it is very simple, specify the environment variables under the development machine or server ASPNETCORE_ENVIRONMENT
, after setting the environment variables, dotnet *.dll
when executing the startup program, ASP. NET Core will automatically load the corresponding file of this environment variable appsettings.*.json
, for example appsettings.Production.json
.
In fact, when we debug a project with VS F5, we also set the ASPNETCORE_ENVIRONMENT
environment variables by default, such as the example configuration in an 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" Span class= "fu": truelaunchurl "" api/values " environmentvariables" : "aspnetcore_environment" : "development" "applicationurl" Span class= "fu": "http://localhost:59522" } /span>
Startup
Example configuration:
Public startup (IHostingEnvironment env) {var builder = new configurationbuilder (). setbasepath (Env.addjsonfile ( "Appsettings.json", Optional: false, Reloadonchange: true). addjsonfile ($ "appsettings.{ Env. Environmentname}.json ", Optional: true). build ();}
As configured above, we are using the ASPNETCORE_ENVIRONMENT
Development
VS F5 Debug Project, the configuration file under the project is loaded and used, and appsettings.Development.json
if this file is not present, ASP. NET Core uses the appsettings.json
configuration file by default.
So how do we set environment variables on the server ASPNETCORE_ENVIRONMENT
? It's very simple, just make a command.
1. Windows Server Settings
Command line:
>setx ASPNETCORE_ENVIRONMENT "Development"SUCCESS: Specified value was saved.
or (requires Administrator privileges)
>setx ASPNETCORE_ENVIRONMENT "Development" /MSUCCESS: Specified value was saved.
PowerShell
Command:
$Env:ASPNETCORE_ENVIRONMENT = "Prodction"
After Windows sets the environment command, you need to open a command line to dotnet *.dll
start the project again before it is valid.
2. Macos/linux Server Settings
Command line:
export ASPNETCORE_ENVIRONMENT=development
dotnet *.dll
When starting the project, we can see the current Hosting environment
, so that the check is correct, example:
> dotnet AspNetCore.Samples.dllHosting environment: ProdtctionContent root path: C:\Users\yuezh\Desktop\Demo\AspNetCore.SamplesNow listening on: http://*:5003Application started. Press Ctrl+C to shut down.
Resources:
- Dotnet-publish
- Working with multiple environments
- How to set the hosting environment in ASP.
ASP. NET Core specified environment release (hosting environment)