zoukankan      html  css  js  c++  java
  • asp.net core2 mvc 基础教程--读取配置文件

    绑定json 配置文件

    var builder = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json")
        .AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true).Build();

    SetBasePath 方法设置 文件目录 可选 不设置 则默认是 web 运行路径
    AddJsonFile 设置要读取的josn 文件
    reloadOnChange 为true 表示 当json文件被修改时重新加载      
    AddJsonFile 也可以用  .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true }) 来代替

    多个配置源时 配置键相同时,读取选择最后一个添加的文件(AddJsonFile)。






    若要读取所有添加文件的配置信息,可遍历ConfigurationRoot的Providers属性。
     var builder = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json")
                    .AddJsonFile("appsettings.Development.json");
                var config = builder.Build();
    
                foreach (var provider in config.Providers)
                {
                    provider.TryGet("ConnectionStrings:DefaultContext", out string defaultContext);
                    Console.WriteLine(defaultContext);
                }
    
    

    config 绑定后有多种读取方式

    第一种 Configuration["key"] 多级加: Configuration["key:key"]

    第二种Configuration .GetSection("key") 多级 继续 .GetSection("key")  例子:Configuration.GetSection("key").GetSection("key").Value;

    第三种 Configuration.GetValue<int>("key"); Configuration.GetValue<int>("key:key")

    第四种 Configuration.GetSection("key").Get<Node>();  绑定到对象 绑定集合 Configuration.GetSection("key").Get<IEnumerable<node>>()  绑定到集合上

    第五种 Options 模式  在services 注册

    public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<Theme>(Configuration.GetSection("Theme"));
    
            services.AddControllersWithViews();  //3.0中启用的新方法
        }
    public class ValuesController : Controller
    {
        private IOptions<Theme> _options = null;
        public ValuesController(IOptions<Theme> options)
        {
            _options = options;
        }
    
        public ContentResult GetOptions()
        {
            return new ContentResult() { Content = $"options:{ _options.Value.Name}" };
        }
    }

    利用 IOptions 来获取注入的配置类

    下面是自定义配置源并注册

      IConfiguration _configuration;
                var builder = new ConfigurationBuilder();
                builder.SetBasePath(Directory.GetCurrentDirectory());
                _configuration = builder.Add(new JsonConfigurationSource { Path = "my.json", Optional = false, ReloadOnChange = true }).Build();
                services.Configure<node>(_configuration.GetSection("key1"));

    也可以直接在 program.cs 文件 里 找到 CreateDefaultBuilder 后加 ConfigureAppConfiguration

      WebHost.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration(builder =>
            {
                builder.AddJsonFile("appconfiguration.json");
            })
            .UseStartup<Startup>();
  • 相关阅读:
    Retrofit2.0+OkHttp打印Request URL(请求地址参数)
    Java如何从HttpServletRequest中读取HTTP请求的body
    解决gradle:download特别慢的问题
    20180531
    20180531 运算符重载
    20180531 二叉树
    20180530
    20180529-2
    20180529-1
    20180529
  • 原文地址:https://www.cnblogs.com/cqqinjie/p/13081516.html
Copyright © 2011-2022 走看看