Talking Appsettings.json in ASP.

Source: Internet
Author: User

In ASP. Three runtime environment variables are provided by default, by looking at the hosting source code we can see, respectively, development, Staging, Production

public static class environmentname{public    static readonly string development = "development";    public static readonly String Staging = "Staging";    public static readonly String Production = "Production";}

When you start an ASP. NET core application, you determine which environment should be running. By default, if no environment variable is specified, it is automatically default to production

public class hostingenvironment:ihostingenvironment, extensions.hosting.ihostingenvironment{public    string environmentname {get; set;} = Hosting.EnvironmentName.Production;    public string ApplicationName {get; set;}    public string Webrootpath {get; set;}    Public Ifileprovider Webrootfileprovider {get; set;}    public string Contentrootpath {get; set;}    Public Ifileprovider Contentrootfileprovider {get; set;}}

In the newly created ASP. NET Core Web application, we see two configuration files, namely Appsettings.json and Appsettings.Development.json. In fact, we can add two files, respectively, appsettings. Staging.json and Appsettings.Production.json, respectively, are pre-production and production environments. Depending on the name of the environment variable, the specific configuration file is loaded.

Config. Addjsonfile ("Appsettings.json", Optional:true, Reloadonchange:true)      . Addjsonfile ($ "appsettings.{ Env. Environmentname}.json ", Optional:true, Reloadonchange:true);

These files can be understood in the form of a face object, Appsettings.json as the parent class, and several other files as subclasses, when the same node is defined in two configuration files, the configuration of the subclass will prevail, equivalent to Orverwrite. If a node is not found in the corresponding configuration file, it is looked up from the parent class.

If our application is running in a Docker container, Docker also allows the use of env to specify environment variables in Dockerfile

From Microsoft/aspnetcore:2.0.5workdir/appexpose 80COPY. . ENV aspnetcore_environment productionentrypoint ["dotnet", "WebApplication1.dll"]

Another way is to use the-e parameter when you run the container

Docker run-e aspnetcore_environment=development-p 5000:5000 <image>:<tag>

Talking Appsettings.json in ASP.

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.