ASP.NETCORE_配置_Option方式二

2024-10-14 14:57:22

1、配置文件加入json内容{ "name": "BeJson", "url": "http://www.bejson.com", "page": 88, "isNonProfit": true, "address": { "street": "科技园路.", "city": "江苏苏州", "country": "中国" }, "links": [ { "name": "Google", "url": "http://www.google.com" }, { "name": "Baidu", "url": "http://www.baidu.com" }, { "name": "SoSo", "url": "http://www.SoSo.com" } ]}

ASP.NETCORE_配置_Option方式二

2、新增类与配置文件对应

ASP.NETCORE_配置_Option方式二

3、加载配置到实例 services.Configure<json1>(Configuration);

ASP.NETCORE_配置_Option方式二

4、view视图依赖注入@using Microsoft.Extensions.Options;@inject IOptions<ASP.NETCORE_配置_Json.json1> bbb@{ ViewData["Title"] = "test";}<h2>@bbb.Value.name</h2><h2>@bbb.Value.links[0].url</h2>

ASP.NETCORE_配置_Option方式二

5、运行完成

ASP.NETCORE_配置_Option方式二
猜你喜欢