ASP.NET CORE读取APPSETTINGS.JSON的配置
程序员文章站
2022-06-02 22:23:45
...
1、首先在appsettings文件中添加我们的其他常用配置信息
2、
再创建一个实体类Setting
3、startup.cs启动类中public void ConfigureServices方法增加
4、在控制器中中定义上下文及setting
5、控制器中访问:_appSettings.value.属性 就可以出来了
参考:http://blchen.com/asp-net-read-config-from-appsettings-json/
"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/
推荐阅读
-
创建基于ASP.NET core 3.1 的RazorPagesMovie项目(一)-创建和使用默认的模板
-
使用NuGet将我们的ASP.NET Core类库打包并将程序包(类库)发布到NuGet平台上进行管理
-
创建基于ASP.NET core 3.1 的RazorPagesMovie项目(三)-已搭建基架的Razor页面解释和更新
-
【架构篇】ASP.NET Core 基于 Consul 动态配置热更新
-
asp.net core系列 24 EF模型配置(主键,生成值,最大长度,并发标记)
-
VS2015 搭建Asp.net core开发环境的方法
-
解决asp.net core在输出中文时乱码的问题
-
asp.net core中灵活的配置方式详解
-
ASP.NET Core环境变量和启动设置的配置教程
-
Aso.Net Core 的配置系统Configuration