ASP. NET Core specified environment release (hosting environment)

Source: Internet
Author: User
Tags dotnet hosting

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>                 

StartupExample 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.

PowerShellCommand:

$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 *.dllWhen 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.