欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ASP.NET CORE读取APPSETTINGS.JSON的配置

程序员文章站 2022-06-02 22:23:39
...
1、首先在appsettings文件中添加我们的其他常用配置信息

  "Setting": {
    "oa_ff": "https://www.oa.com/",
    "crm_ff":"123465"
  }


2、
再创建一个实体类Setting
public class Setting
    {
        public string oa_ff { get; set; }
        public string crm_ff{ get; set; }
    }


3、startup.cs启动类中public void ConfigureServices方法增加
services.AddOptions();
            services
                .Configure<Setting>(Configuration.GetSection("Setting"));  //此处的Setting类是自己新建的,内容要和appsetting.json中Setting节点里字段对应起来方便以后访问


4、在控制器中中定义上下文及setting
//定义上下文
        private readonly db_oa_depContext _context;
        private readonly IOptions<Setting> _appSettings;

        public WorkFlowController(db_oa_depContext context, IOptions<Setting> appSettings)
        {
            this._context = context;
            this._appSettings = appSettings;
        }


5、控制器中访问:_appSettings.value.属性  就可以出来了

参考:http://blchen.com/asp-net-read-config-from-appsettings-json/