.net core 3.0 + angular 8.0 ----Project creation process

1. Read the appsetting.json data for the entire project, and use the corresponding configuration file in the corresponding environment.

Official documentation:Configuration in ASP.NET Core | Microsoft Learn

When creating a .net core application, it already existsIConfiguration,我们只是在需要使用的地方声明使用即可(IConfiguration.GetConnectionString("your using name").

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

ASP.NET Core reads the environment variable ASPNETCORE_ENVIRONMENT when the application starts. ASPNETCORE_ENVIRONMENT can be set to any value, but the framework only supports three values: Development, Staging and Production, so we first add appsettings.Development.json and appsettings.Production to the project. .json and appsettings.Staging.json for backup.

The appsettings.json generated by the project by default is used to store public configurations. When we set up the Development environment, the final configuration item is the result of the union of appsettings.Development.json and appsettings.json. If the two files have configuration items with the same name, appsettings .Development.json shall prevail, and the same applies to other environments. The configuration items of each json file are as follows


/*appsettings.json*/
{
  "UserType": "default",
  "OnlyDefault": "onlyDefault"
}
 
/*appsettings.Development.json*/
{
{
  "UserType": "development",
  "OnlyDevelopment": "onlyDevelopment"
}
 
/*appsettings.Production.json*/
{
  "UserType": "production",
  "OnlyProduction": "onlyProduction"
}
 
/*appsettings.Staging.json*/
{
  "UserType": "staging",
  "OnlyStaging": "onlyStaging"

}

If ASPNETCORE_ENVIRONMENT is set to Development, then the data of appsettings.Development.json will be read when debugging in VS
No matter ASPNETCORE_ENVIRONMENT is set to Development, Staging, or Production, as long as the project If there is appsettings.Production.json, the Production configuration will be read by default when running after the project is released.

When developing locally, just make sure it is in development.

Guess you like

Origin blog.csdn.net/VS18703761631/article/details/105093450