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 ...
随机推荐
- oracle Group by 分组查询后,分页
------------恢复内容开始------------ 1.分页查询 select count(*) times,title from menulog group by title order ...
- @ControllerAdvice全局异常处理不起作用原因及解决办法
这段时间使用springboot搭建基础框架,作为springboot新手,各种问题都有. 当把前端框架搭建进来时,针对所有controller层的请求,所发生的异常,需要有一个统一的异常处理,然后返 ...
- ifix 自动化(Automation)错误弹窗的解决方案
在先前ifix项目中添加了语音模块,然后概率性跳出自动化(Automation)错误弹窗,先前分析了很多种原因,从代码的冗余,编码等角度进行了优化,效果不是很理想,仍然会概率性出现.经过反反复复大约3 ...
- xshell中操作服务器笔记
sudo su 获取root权限 cd 切换到相应文件夹 ll ls 查看文件夹内容 cp file folder 复制文件到文件夹 \cp为强制覆盖不提示 cp -r /packageA/* /cp ...
- linux下利用JMX监控Tomcat
利用JMX监控Tomcat,就是相当于部署在tomcat上的应用作为服务端,也就是被管理资源的对象.然后通过程序或者jconsole远程连接到该应用上来.远程连接需要服务器端提供ip和port.如果需 ...
- 卷积的等变性(equivariant) / 不变性(invariant)
不变性:输入x发生变换,但是F之后的输出不变 \(F(x)=F [\)transform\((x)]\) 池化:近似不变性,当图像发生微小变化,最大池化的输出不变,还是一个池化范围内的max 等变性: ...
- 【LeetCode】209. 长度最小的子数组
209. 长度最小的子数组 知识点:数组:前缀和:二分法:双指针:滑动窗口 题目描述 给定一个含有 n 个正整数的数组和一个正整数 target . 找出该数组中满足其和 ≥ target 的长度最小 ...
- Linux 基础指令初识
Linux 基础指令初识 01. ls 指令 语法: ls [选项] [目录或文件] 功能:对于目录,该命令列出该目录下的所有子目录与文件.对于文件,将列出文件名以及其他信息 -a 列出目录下的所有文 ...
- 在nodejs中利用 Proxy监听对象值的获取
1 window = new Proxy(global, { 2 get: function (target, key, receiver) { 3 console.log("window. ...
- Salesforce Integration 概览(五) Remote Call-In(远程操作 外部->salesforce)
本篇参考:https://resources.docs.salesforce.com/sfdc/pdf/integration_patterns_and_practices.pdf 本篇博客介绍 Re ...