ASP.NET Core 3.X IConfiguration取值

当从配置中取设置的变量值的话,可以从appsettings.json/appsettings.Development.json/自定义的类以及环境变量中取

从appsettings.json中获取
public void Configure(IApplicationBuilder app, 
            IWebHostEnvironment env,
            IConfiguration configuration,
            IWelcome iWelcome)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            //app.UseWelcomePage();

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    var welcome = configuration["welcome"];
                    //var welcome = iWelcome.GetMessage();
                    await context.Response.WriteAsync(welcome);
                });
            });
        }

在appsetting.json中增加一个变量welcome

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "welcome": "hello welcom from appsetting.json"
}

此时,点击测试,可以显示从appsettings.json中配置的变量

从appsettings.Development.json中取值

如果appsetting.json和appsettings.Development.json都存在同一变量的值,那么最终会从appsettings.Development.json中获取

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "welcome": "hello welcom from appsetting.json"
}

appsettings.Development.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "welcome": "hello world from appseting from development"
}

测试显示结果
在这里插入图片描述

从自定义的类中获取变量值

自定义接口

 public interface IWelcome
    {
        string GetMessage();
    }

接口实现

public class welcome:IWelcome
    {
        public string GetMessage()
        {
            return "hello world from service";
        }
    }

启动服务注册,如果不进行注册,是无法直接使用依赖注入的功能的

  public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<IWelcome, welcome>();
        }

然后在IConfiguration中将这个接口注入

 public void Configure(IApplicationBuilder app, 
            IWebHostEnvironment env,
            IConfiguration configuration,
            IWelcome iWelcome)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            //app.UseWelcomePage();

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    //var welcome = configuration["welcome"];
                    var welcome = iWelcome.GetMessage();
                    await context.Response.WriteAsync(welcome);
                });
            });
        }

然后测试
在这里插入图片描述

还可以通过电脑上的环境变量来获取,这里就不演示了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值