ASP.NET Core application publishing command example

零下一度
Release: 2017-06-24 10:46:45
Original
2225 people have browsed it

ASP.NET Core application release command:

dotnet publish [<PROJECT>] [-f|--framework] [-r|--runtime] [-o|--output] [-c|--configuration] [--version-suffix] [-v|--verbosity] [-h|--help]
Copy after login

Publish sample command (generated in the bin/release/netcoreapp1.1/publish directory):

dotnet publish -c release
Copy after login

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.

ASP.NET Core application publishing command example

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.jsonSample 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"}}
Copy after login

StartupSample 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();
}
Copy after login

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.

1. Windows server settings

Command line:

>setx ASPNETCORE_ENVIRONMENT "Development"SUCCESS: Specified value was saved.
Copy after login

or (requires administrator rights)

>setx ASPNETCORE_ENVIRONMENT "Development" /MSUCCESS: Specified value was saved.
Copy after login

PowerShellCommand:

$Env:ASPNETCORE_ENVIRONMENT = "Prodction"
Copy after login

Windows After setting the environment command, you need to reopen a command line dotnet *.dll to start the project to be effective.

2. MacOS/Linux server settings

Command line:

export ASPNETCORE_ENVIRONMENT=development
Copy after login

dotnet *.dllWhen 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.
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!