Asp.Net Core 读取配置文件的方式

一定要注意的是 配置文件要设置成 始终复制,才能把配置文件复制到运行目录,否则读取不到配置文件

 以下代码是在控制台程序中使用加载的,

 class Program
    {
        static void Main(string[] args)
        {
            ServiceCollection sc = new ServiceCollection();
            sc.AddScoped<controller>();
            sc.AddScoped<proxyExtend>();

            ConfigurationBuilder cb = new ConfigurationBuilder();
            cb.AddJsonFile("config.json", true, reloadOnChange: true);
            cb.AddJsonFile("json1.json", true, true);
            IConfigurationRoot cbRoot = cb.Build();

            sc.Configure<Config>(x=>cbRoot.Bind(x))
                .Configure<Proxy>(e => cbRoot.GetSection("Proxy").Bind(e));

            using (var sp = sc.BuildServiceProvider())
            {
                var ct = sp.GetRequiredService<controller>();

                ct.test();
                var json1 = sp.GetRequiredService<proxyExtend>();
                Console.WriteLine(json1.OptExtent.Value.Address);
            }

            //Console.WriteLine(cbRoot["name"]);
            //Console.WriteLine(cbRoot.GetSection("proxy:address").Value);
            //var cf = cbRoot.Get<Config>();
            //Console.WriteLine(cf.Name);
            //Console.WriteLine(cf.proxy.Address);

            Console.ReadLine();
        }
    }
    public class controller
    {
        private readonly IOptionsSnapshot<Config> optConfig;
        public controller(IOptionsSnapshot<Config> optConfig)
        {
            this.optConfig = optConfig;
        }
        public void test()
        {
            Console.WriteLine(optConfig.Value.Age);
            Console.WriteLine("***********************");

            Console.WriteLine("Name="+optConfig.Value.Name);

            Console.WriteLine("port="+optConfig.Value.port);
            Console.WriteLine("Address="+optConfig.Value.Address);
        }
    }
    public class proxyExtend
    {
        private readonly IOptionsSnapshot<Proxy> optPro;
        public proxyExtend(IOptionsSnapshot<Proxy> optPro)
        {
            this.optPro = optPro;
        }
        public IOptionsSnapshot<Proxy> OptExtent { get { return optPro; } }
    }

    public class Config
    {
        public string? Name { get; set; }
        public int Age { get; set; }
        public string? Address { get; set; }
        public int port { get; set; }
        public Proxy? proxy { get; set; }
    }
    public class Proxy
    {
        public string? Address { get; set; }
        public int port { get; set; }
    }

 

Config.json

{
  "Name": "zxq",
  "Age": 18,
  "Proxy": {
    "Address": "127.0.0.1",
    "Port": 90
  }
}

json1.json

{
  "Address": "127.0.0.2",
  "Port": 8080
}

 

在Asp.net Core WebApi项目中,系统已经替我们提前默认加载了配置文件,不需要我们再写了

 

 

 

 

 

 

Part4-33:ASP.NETcore与配置系统的集成_哔哩哔哩_bilibili

可以参考的两篇文章:

ASP.NET Core中的配置Configuration的使用及其源码解析 - 谢友海 - 博客园 (cnblogs.com)

ASP.NET Core中Options模式的使用及其源码解析 - 谢友海 - 博客园 (cnblogs.com)

(36条消息) ASP.NET Core学习日记36_阿升1990的博客-CSDN博客

ASP.NET Core 基础系列(5)(appSetting) - 痕迹g - 博客园 (cnblogs.com)

.net5 core webapi项目实战之十八:配置文件的读取 - 屏风马 - 博客园 (cnblogs.com)

posted @ 2022-07-14 09:15  百年俊少  阅读(531)  评论(0编辑  收藏  举报