Asp.NetCore3.1 WebApi 获取配置json文件中的数据
下面只是做一个简单的测试:
1:定义好appsetting.Json文件的配置信息如下:

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Appset01": {
"name": "小张",
"age": "18",
"sex": "男"
},
"personnel": {
"name": "小张",
"addr": "湖南怀化",
"books": [
{
"bookid": "b001",
"bname": "西游记"
},
{
"bookid": "b002",
"bname": "水浒传"
}
]
},
"AllowedHosts": "*"
}
2:根据内容获取或者设置实体来:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace WebApiDemo
{
public class Appset01
{
public string name { get; set; }
public string age { get; set; }
public string sex { get; set; }
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace WebApiDemo
{
public class Person
{
public string name { get; set; }
public string addr { get; set; }
public Book[] books { get; set; }
} public class Book
{
public string bookid { get; set; }
public string bname { get; set; }
}
}
3:Startup内容中配置好服务信息:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options; namespace WebApiDemo
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.Configure<Person>(Configuration.GetSection("personnel"));
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env,IOptions<Appset01>appOptions)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
4:在Controller中使用:

using System;
namespace WebApiDemo.Controllers
{
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
[ApiController]
[Route("api/[controller]")]
public class AppSetjsonController : ControllerBase
{
private IConfiguration _Configuration;
public Person _person { get; set; }
public Appset01 _jwtobj { get; set; }
public AppSetjsonController(IConfiguration configuration, IOptions<Person> options)
{
this._Configuration = configuration;
this._person = options.Value;
}
[HttpGet]
[Route("getAppSetting")]
public ApiResult<Appset01> getAppSetting()
{
ApiResult<Appset01> dic = new ApiResult<Appset01>();
try
{
//BindNonPublicProperties 默认为false全部获取,true为不获取私有的字段
dic.data = _Configuration.GetSection("Appset01").Get<Appset01>(c => c.BindNonPublicProperties = true);
Console.WriteLine("addr=" + _person.addr + ",name=" + _person.name + ", book[0].name=" + _person.books[0].bname);
dic.message = "获取成功!";
}
catch (Exception ex)
{
dic.message = "获取失败:" + ex.Message;
}
return dic;
}
}
}
5:查询的效果展示:

6:获取自定义路径下的json配置文件信息
其他方法获取(下标)

using Microsoft.Extensions.Configuration;
public IConfiguration _configuration { get; set; }
public BasisApiResult doJson(int index)
{
BasisApiResult result = new BasisApiResult();
List<Dictionary<string, string>> dict = new List<Dictionary<string, string>>();
_configuration.GetSection("OSS").Bind(dict);
dict[index].TryGetValue("AccessKeyId", out string qq);
Console.WriteLine("qq=" + qq);
result.data = qq;
return result;
}
Asp.NetCore3.1 WebApi 获取配置json文件中的数据的更多相关文章
- 接口自动化(三)--读取json文件中的数据
上篇讲到实际的请求数据放置在json文件内,这一部分记述一下python读取json文件的实现. 代码如下(代码做了简化,根据需要调优:可做一些容错处理): 1 import json 2 3 cla ...
- Android中通过代码获取arrays.xml文件中的数据
android工程res/valuse文件夹下的arrays.xml文件中用于放各种数组数据,比如字符串数组.整型数组等,数组中的数据可能是具体的值,也有可能是对资源数据的引用,下面针对这两种情况通过 ...
- NetCore 获取appsetting.json 文件中的配置
1. using Microsoft.Extensions.Configuration public class HomeController : Controller { public IConfi ...
- [数据科学] 从text, json文件中提取数据
文本文件是基本的文件类型,不管是csv, xls, json, 还是xml等等都可以按照文本文件的形式读取. #-*- coding: utf-8 -*- fpath = "data/tex ...
- ASP.NET Core 在 JSON 文件中配置依赖注入
前言 在上一篇文章中写了如何在MVC中配置全局路由前缀,今天给大家介绍一下如何在在 json 文件中配置依赖注入. 在以前的 ASP.NET 4+ (MVC,Web Api,Owin,SingalR等 ...
- 转载:ASP.NET Core 在 JSON 文件中配置依赖注入
在以前的 ASP.NET 4+ (MVC,Web Api,Owin,SingalR等)时候,都是提供了专有的接口以供使用第三方的依赖注入组件,比如我们常用的会使用 Autofac.Untiy.Stri ...
- 谷歌通过ajax获取本地JSON文件,为什么会显示跨域?转载的
在本地写了一段JSON代码,然后用ajax读取后,在浏览器打开,发现谷歌提示涉及到跨域问题, 但是跨域是由于协议,域名,端口中有一个不同,才会跨域,我在本地访问自己的文件,怎么和跨域扯上关系了?? 谷 ...
- 谷歌通过ajax获取本地JSON文件,为什么会提示跨域?
在本地写了一段JSON代码,然后用ajax读取后,在浏览器打开,发现谷歌提示涉及到跨域问题, 但是跨域是由于协议,域名,端口中有一个不同,才会跨域,我在本地访问自己的文件,怎么和跨域扯上关系了?? 下 ...
- 六种获取配置properties文件的方法
总结一下六种获取配置properties文件的方法,代码如下: package com.xujingyang.test ; import java.io.BufferedInputStream ; i ...
随机推荐
- Web 字体 font-family 浅谈
前言 最近研究各大网站的font-family字体设置,发现每个网站的默认值都不相同,甚至一些大网站也犯了很明显的错误,说明字体还是有很大学问的,值的我们好好研究. 不同的操作系统.不同浏览器下内嵌的 ...
- 一键设置WPS_Office_2019专业版的定时自动备份的批处理文件
一键设置WPS_Office_2019专业版的定时自动备份的批处理文件 rem ================================================ rem 一键设置WPS ...
- CSS 即将支持嵌套,SASS/LESS 等预处理器已无用武之地?
最近,有一则非常振奋人心的消息,CSS 即将原生支持嵌套 -- Agenda+ to publish FPWD of Nesting,表示 CSS 嵌套规范即将进入规范的 FWPD 阶段. 目前对应的 ...
- 什么是 RFC 2544
什么是 RFC 2544? 如果您从事网络工作,您可能听说过它,但 RFC 2544 究竟是什么呢? RFC 的全称是 Request for comment ,请求注解.是一系列收录了互联网国际标准 ...
- linux笔记全(无图版)
1.ls 查看当前目录下的所有内容 黑色的是文件,蓝色的是文件夹,也就是目录 2.rm -f anaconda-ks. cfg 彻底删除文件(如不确定,则需要先保存备份,也就是快照) 3.ifconf ...
- web笔记随笔
1.Web服务组件 8.第三方内容:广告统计.mockup 7.Web前端框架: HTML5. jQuery. Bootstrap 6.Web应用: CMS. BBS. Blog 5.Web开发框架: ...
- 面试必知道的APP测试adb命令
查看当前连接设备: adb devices 如果发现多个设备: adb -s 设备号 其他指令 查看日志: adb logcat 安装apk文件: adb install xxx.apk 此安装方式, ...
- GUI编程简介
GUI编程(淘汰) GUI编程怎么学? 这是什么 它怎么玩 该如何去在我们平时运用 class -- 可阅读 组件 窗口 弹窗 面板 文本框 列表框 按钮 图片 监听事件 鼠标 键盘事件 破解工具 1 ...
- Go语言笔记[实现一个Web框架实战]——EzWeb框架(一)
Go语言笔记[实现一个Web框架实战]--EzWeb框架(一) 一.Golang中的net/http标准库如何处理一个请求 func main() { http.HandleFunc("/& ...
- pikachu RCE远程系统命令执行
远程系统命令执行 一般出现这种漏洞,是因为应用系统从设计上需要给用户提供指定的远程命令操作的接口比如我们常见的路由器.防火墙.入侵检测等设备的web管理界面上一般会给用户提供一个ping操作的web界 ...