博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.net core 中如何读取 appsettings.json 相关配置
阅读量:4498 次
发布时间:2019-06-08

本文共 1202 字,大约阅读时间需要 4 分钟。

appsettings.json如下

{  "Logging": {    "LogLevel": {      "Default": "Debug",      "System": "Information",      "Microsoft": "Information"    },    "AllowedHosts": "*"  },  "ConfigSetting": {    "Ctu": 1,    "Btu": "Btu",    "Atu": "Atu"  }}

建立实体ConfigSetting

public class ConfigSetting    {        public int Ctu { get; set; }        public string Btu { get; set; }        public string Atu { get; set; }    }

Startup配置

public void ConfigureServices(IServiceCollection services)        {            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);            services.Configure
(Configuration.GetSection("ConfigSetting")); }

Controller配置

[Route("api/[controller]")]    [ApiController]    public class ValuesController : ControllerBase    {        private readonly IOptions
_ConfigSettingt; public ValuesController(IOptions
ConfigSetting) { _ConfigSettingt = ConfigSetting; } [HttpGet("{id}")] public ActionResult
Get(int id) { return _ConfigSettingt.Value.Atu; } }

 

转载于:https://www.cnblogs.com/zacklau/p/11598955.html

你可能感兴趣的文章
实验6-数组(1)
查看>>
Ubuntu --- not enough free disk space
查看>>
HDU3068(Manacher算法)
查看>>
Mysq数据库备份(win)
查看>>
2018-04-21 搭建Python官方文档翻译环境
查看>>
第一个 手动写Servlet
查看>>
WinForm自定义Loading控件
查看>>
C++ 通过对象方式 、指针方式两种方式去访问成员变量(属性或者方法)
查看>>
JS根据key值获取URL中的参数值,以及把URL的参数转换成json对象
查看>>
HDU 5496 Beauty of Sequence
查看>>
HDU 5656 CA Loves GCD 01背包+gcd
查看>>
BZOJ 1854: [Scoi2010]游戏 无向图判环
查看>>
php从数组中取出一段 之 array_slice
查看>>
Python操作文件-20181121
查看>>
Angular之constructor和ngOnInit差异及适用场景(转)
查看>>
Extjs4实现客户端搜索(过滤数据)
查看>>
学习TF:《TensorFlow技术解析与实战》PDF+代码
查看>>
3.4 变量
查看>>
[转]Timer和TimerTask
查看>>
MAC下Android的Eclipse开发环境的搭建
查看>>