从零开始学习ASP.NET CORE(四)appsettings.json

ASP.NET Core中的配置源

  • Appsettings.json,appsettings.{Environment}.json,不同环境下对应不同的托管环境
  • User secrets(用户机密)
  • Environment variables(环境变量)
  • Command-line arguments(命令行参数)

上面的配置如何配置和如何获取如下,下面都会以text为key,value有所不同

Appsettings.json

1、配置,打开项目里面的Appsettings.json文件,新增下面的代码

{
  .......
  "text": "appsettings.json的text"
}

2、获取代码如下

{
    public class Startup
    {
        private readonly IConfiguration _configuration;

        public Startup(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        .......

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            .....

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    var text = _configuration["text"];
                    await context.Response.WriteAsync(text);
                });
            });
        }
    }
}

appsettings.Development.json

1、配置,打开项目里面的Appsettings.json下面的appsettings.Development.json文件,新增下面的代码

{
  ......
  "text": "appsettings.json的text"
}

2、获取同上面一样

User secrets

1、配置,右键项目,点击“管理用户机密”,新增下面的代码

{
  "text": "secret的text"
}

这里补充一点,新增保存了以后,可以看到这个文件生成在我们用户电脑的本地
2、获取同上面一样

Environment variables

1、配置,在上一期讲过的launchSettings.json文件里面,新增代码如下

{
  ......
  "profiles": {
    "IIS Express": {
      ........
      "environmentVariables": { 
        ......
        "text": "launchSettings.json的text"
      }
    },
    .........
  }
}

2、获取同上面一样

Command-line arguments

1、配置,在上一期讲过的命令行,命令如下

dotnet run text="command的text"

2、获取同上面一样

加载顺序

加载配置,如果出现相同的key,会进行覆盖,覆盖顺序,从上到下如下

  • Appsettings.json,appsettings.{Environment}.json,不同环境下对应不同的托管环境
  • User secrets(用户机密)
  • Environment variables(环境变量)
  • Command-line arguments(命令行参数)

可以从微软的asp.core的开源代码里面查看如下链接: https://github.com/aspnet/MetaPackages/blob/release/2.2/src/Microsoft.AspNetCore/WebHost.cs

 .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    var env = hostingContext.HostingEnvironment;

                    config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                          .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

                    if (env.IsDevelopment())
                    {
                        var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
                        if (appAssembly != null)
                        {
                            config.AddUserSecrets(appAssembly, optional: true);
                        }
                    }

                    config.AddEnvironmentVariables();

                    if (args != null)
                    {
                        config.AddCommandLine(args);
                    }
                })

可以看出上述一段代码,里面就是微软底层代码判断的顺序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值