.net core 3.0 + angular 8.0 ----项目创建过程

1. 读取appsetting.json数据为整体项目使用,以及在对应的环境下使用对应的配置文件。

官方文档:ASP.NET Core 中的配置 | Microsoft Learn

创建.net core application时,已经存在IConfiguration,我们只是在需要使用的地方声明使用即可(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在应用启动时读取环境变量ASPNETCORE_ENVIRONMENT,ASPNETCORE_ENVIRONMENT可以设置任意值,但框架仅支持三个值:Development、Staging 和 Production,因此我们先在项目中添加appsettings.Development.json、appsettings.Production.json和appsettings.Staging.json以备用。

项目默认生成的appsettings.json用来存放公共配置,当我们设置Development环境时,最终的配置项是appsettings.Development.json和appsettings.json求并集的结果,若两文件有同名配置项则以appsettings.Development.json为准,其他环境同理。各json文件的配置项如下


/*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"

}

ASPNETCORE_ENVIRONMENT设置为Development,那在VS中调试的时候就会读取appsettings.Development.json的数据
无论 ASPNETCORE_ENVIRONMENT设置为Development、Staging、Production,只要项目中有appsettings.Production.json,那项目发布后运行时默认会读取Production的配置。

在本地开发时,保证是在development即可。

猜你喜欢

转载自blog.csdn.net/VS18703761631/article/details/105093450