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 ...
随机推荐
- P5147-数学-随机数生成器
P5147-数学-随机数生成器 (洛谷第一篇题解说这是高一数学题,新高二感觉到被吊打) 我们设work(x)的期望值为\(f_x\) 注意\(f_1\)是边界.不过对下列式子没有影响.原因参照必修的数 ...
- Spring,Spring-boot(一)
前言 Spring作为java开源世界第一开源框架,已经成为事实上的Java EE开发标准. 最根本的使命就是简化Java开发. 不重复制造车轮 Don't reinvent the wheel .从 ...
- 在Java web项目中防止用户注销后使用浏览器中的“后退”按钮返回注销前页面
一背景 公司安全整改, 要求:系统中对于关键业务操作应确保使用浏览器"后退"功能无法回到上一步操作界面. 提供:凭证提供所有被检查系统关键业务操作后回退视频,视频显示关键业务操作后 ...
- Git的使用(六)
前言 版本管理工具总结: 开发团队项目,对项目的版本进行管理. 使用过的版本管理工具: TFS.SVN与Git. TFS:管理项目,通过visual Studio管理源码,拉取分支,提交代码等.也可以 ...
- Beego和Vue的前后端分离跨域问题处理
VUE封装的请求头(注意请求头,跨域要用到) 路径 utils/mereq.js import request from '@/utils/request' import qs from 'qs' e ...
- Selenium环境搭建 - Mac电脑
一. JDK安装 1.1.官网下载1.8版本 可参考以下链接步骤: 'https://blog.csdn.net/u014801367/article/details/86288078' 1.2.jd ...
- vue.js 贡献指南(翻译)
Vue.js Contributing Guide vue 2.x 嗨! 我很高兴你有兴趣为Vue.js做贡献. 在提交您的贡献之前,请务必花点时间阅读以下指南. 行为守则 问题报告指南 PR指南 开 ...
- 【LeetCode】81. 搜索旋转排序数组 II
81. 搜索旋转排序数组 II 知识点:数组,二分查找: 题目描述 已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同. 在传递给函数之前,nums 在预先未知的某个下标 k(0 ...
- Netty 源码分析系列(二)Netty 架构设计
前言 上一篇文章,我们对 Netty做了一个基本的概述,知道什么是Netty以及Netty的简单应用. Netty 源码分析系列(一)Netty 概述 本篇文章我们就来说说Netty的架构设计,解密高 ...
- 2019-07-06 sql备忘 连续取最大
连续最大: SELECT M.* FROM #temp MINNER JOIN (SELECT ISNULL(A.score,0)-b.score AS score,B.id FROM #temp A ...