Home > Article > Backend Development > ASP.NET Core application publishing command example
ASP.NET Core application release command:
dotnet publish [<PROJECT>] [-f|--framework] [-r|--runtime] [-o|--output] [-c|--configuration] [--version-suffix] [-v|--verbosity] [-h|--help]
Publish sample command (generated in the bin/release/netcoreapp1.1/publish
directory):
dotnet publish -c release
The above command does not specify EnvironmentName
to publish. What does it mean? For example, the appsettings.json
configuration in the ASP.NET Core application has different configurations between the test environment and the production environment (such as database connection string). If we use the above release command, we still need to manually copy it. If the appsettings.json
files of different environments need to be changed in the future, they need to be published and updated again, which is very troublesome.
How to solve the above problem is very simple. Specify the ASPNETCORE_ENVIRONMENT
environment variable of the development machine or server. After setting the environment variable, execute dotnet *.dll
to start the program. , ASP.NET Core will automatically load the appsettings.*.json
file corresponding to this environment variable, such as appsettings.Production.json
.
In fact, when we use VS 2017 F5 to debug a project, the ASPNETCORE_ENVIRONMENT
environment variable will be set by default, such as in ASP.NET Core applications. The launchSettings.json
Sample configuration:
"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
Sample 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, ASPNETCORE_ENVIRONMENT
is set to Development
. We are using VS 2017 F5 to debug the project, and the appsettings.Development.json
configuration under the project will be loaded and used. file, if this file does not exist, ASP.NET Core will use the appsettings.json
configuration file by default.
So how do we set the ASPNETCORE_ENVIRONMENT
environment variable on the server? It's very simple, just type a command.
Command line:
>setx ASPNETCORE_ENVIRONMENT "Development"SUCCESS: Specified value was saved.
or (requires administrator rights)
>setx ASPNETCORE_ENVIRONMENT "Development" /MSUCCESS: Specified value was saved.
PowerShell
Command:
$Env:ASPNETCORE_ENVIRONMENT = "Prodction"
Windows After setting the environment command, you need to reopen a command line dotnet *.dll
to start the project to be effective.
Command line:
export ASPNETCORE_ENVIRONMENT=development
dotnet *.dll
When starting the project, We can see the current Hosting environment
to check if it 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.
Reference:
dotnet-publish
Working with multiple environments
How to set the hosting environment in ASP.NET Core
The above is the detailed content of ASP.NET Core application publishing command example. For more information, please follow other related articles on the PHP Chinese website!