循序渐进学.Net Core Web Api开发系列【6】:配置文件appsettings.json
系列目录
本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi
一、本篇概述
本篇描述appsettings.json的使用,包括:
1、配置的基本读取
2、读取配置信息到自定义的对象
3、自定义配置文件
一、配置的基本读取
要读取的配置文件内容如下:
{
"ConnString": "MySQL Connect String",
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
},
"SystemConfig": {
"UploadFile": "d:\\files",
"AnnexUrl": "http://192.168.0.177:81/",
"Admin": {
"Name": "admin",
"Age": "",
"Allow": "True"
},
"DefaultPassword": ""
}
}
读取配置文件的方法如下:
public class Startup
{
public Startup(IConfiguration configuration,IHostingEnvironment env)
{
Configuration = configuration;
_env = env;
} public IConfiguration Configuration { get; }
public IHostingEnvironment _env { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(); var ConnString = Configuration["ConnString"];
var UploadFile = Configuration.GetSection("SystemConfig")["UploadFile"];
var AdminName = Configuration.GetSection("SystemConfig").GetSection("Admin")["Name"];
}
}
还有一种读法:
ConnString = Configuration["ConnString"];
UploadFile = Configuration["SystemConfig:UploadFile"];
AdminName = Configuration["SystemConfig:Admin:Name"];
这里Startup类在构造时已经帮我们注入了Configuration,如果要在自己的Controller内使用,需要自己注入。
public class ValuesController : Controller
{
private IConfiguration _configuration; public ValuesController(IConfiguration configuration)
{
_configuration = configuration;
} [HttpGet]
public IEnumerable<string> Get()
{
var ConnString = _configuration["ConnString"];
return new string[] { "value1", "value2" };
}
}
二、读取配置信息到自定义的对象
新建一个类,用来存储配置信息,类的结构应和配置文件一致。
public class SystemConfig
{
public String UploadFile { get; set; }
public String AnnexUrl { get; set; }
public User Admin { get; set; }
public String DefaultPassword { get; set; }
} public class User
{
public String Name { get; set; }
public int Age { get; set; }
public bool Allow { get; set; }
}
在startup类的ConfigureServices方法内,提供如下代码:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(); services.AddOptions();
services.Configure<SystemConfig>(Configuration.GetSection("SystemConfig"));
}
}
}
然后在Controller中进行注入并使用。
public class ValuesController : Controller
{
private IOptions<SystemConfig> _setting; public ValuesController( IOptions<SystemConfig> setting)
{
_setting = setting;
} [HttpGet("setting")]
public IEnumerable<string> GetSetting()
{
var UploadFile = _setting.Value.UploadFile;
var AdminName = _setting.Value.Admin.Name;
var AdminAge = _setting.Value.Admin.Age;
var AdminAllow = _setting.Value.Admin.Allow; return new string[] { "value1", "value2" };
}
}
可以看到,这样在业务方法内通过 _setting 来都取配置信息就非常方便了。
三、自定义配置文件
以上操作的是系统默认的配置文件appsettings.json。如果我们需要增加自己的配置文件该如何处理?
新建一个配置文件:mysetting.json
{
"ConnString": "MySQL Connect String",
"SystemConfig": {
"UploadFile": "d:\\myfiles",
"AnnexUrl": "http://192.168.0.177:81/my",
"Admin": {
"Name": "myadmin",
"Age": "",
"Allow": "False"
},
"DefaultPassword": ""
}
}
在Startup类的ConfigureServices方法输入以下代码:
public class Startup
{
public Startup(IConfiguration configuration,IHostingEnvironment env)
{
Configuration = configuration;
_env = env;
} public IConfiguration Configuration { get; }
public IHostingEnvironment _env { get; } public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(); var rootpath = _env.ContentRootPath;
var builder = new ConfigurationBuilder()
.SetBasePath(_env.ContentRootPath)
.AddJsonFile("mysetting.json",optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
var MyConfiguration = builder.Build(); services.AddOptions();
services.Configure<SystemConfig>(MyConfiguration.GetSection("SystemConfig"));
}
}
用自己创建的Configuration进行服务的注册,创建过程中需要用到IHostingEnvironment,这个对象在Startup类构建时进行注入。
剩下的用法和默认配置用法就一样了。
循序渐进学.Net Core Web Api开发系列【6】:配置文件appsettings.json的更多相关文章
- 循序渐进学.Net Core Web Api开发系列【0】:序言与目录
一.序言 我大约在2003年时候开始接触到.NET,最初在.NET framework 1.1版本下写过代码,曾经做过WinForm和ASP.NET开发.大约在2010年的时候转型JAVA环境,这么多 ...
- 循序渐进学.Net Core Web Api开发系列【16】:应用安全续-加密与解密
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 应用安全除 ...
- 循序渐进学.Net Core Web Api开发系列【15】:应用安全
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍W ...
- 循序渐进学.Net Core Web Api开发系列【14】:异常处理
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍异 ...
- 循序渐进学.Net Core Web Api开发系列【13】:中间件(Middleware)
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍如 ...
- 循序渐进学.Net Core Web Api开发系列【12】:缓存
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍如 ...
- 循序渐进学.Net Core Web Api开发系列【11】:依赖注入
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍如 ...
- 循序渐进学.Net Core Web Api开发系列【10】:使用日志
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.本篇概述 本篇介 ...
- 循序渐进学.Net Core Web Api开发系列【9】:常用的数据库操作
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇描述一 ...
- 循序渐进学.Net Core Web Api开发系列【8】:访问数据库(基本功能)
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇讨论如 ...
随机推荐
- 使用 EXISTS 代替 IN 和 inner join
在使用Exists时,如果能正确使用,有时会提高查询速度: 1,使用Exists代替inner join 2,使用Exists代替 in 1,使用Exists代替inner join例子: 在一般写s ...
- Mac OS利用ssh访问ubuntu虚拟机及云端操作
1.桥接模式 将该虚拟机的网口设置成桥接模式(Bridged Adapter),以确保主机可以ping通虚拟机: 2.安装ssh 在ubuntu虚拟机上安装ssh server: sudo apt-g ...
- Angular的依赖注入(依赖反转)原理说明
依赖注入(依赖反转)意思是由函数决定要引入什么样的依赖: let mod = angular.module('test',[]); mod.controller('test_c',function($ ...
- .net 未被引用的错误
开发的时候遇到了一个错误,如下: 错误 1 类型“System.ServiceModel.ClientBase`1<T0>”在未被引用的程序集中定义. 我原本以为是版本号的问题,添加了引用 ...
- 快速了解yuv4:4:4 yuv4:2:2 yuv 4:1:1 yuv 4:2:0四种YUV格式区别
四种YUV格式区别如下: 1.YUV 4:4:4抽样方式: Y: Y0 Y1 Y2 Y3 U: U0 U1 U2 U3 V: V0 V1 V2 V3 2.YUV 4:2:2抽样方式: Y : ...
- SQL语句(七)简单查询
--简单信息查询 --例1 查询所有学生的信息 --学生 -- Student --所有学生 -- 不限定班级.性别.年龄等条件 --所有信息 -- 所有字段,* select * from stud ...
- ASP.NET MVC学习笔记-----Filter(2)
接上篇ASP.NET MVC学习笔记-----Filter(1) Action Filter Action Filter可以基于任何目的使用,它需要实现IActionFilter接口: public ...
- 【LibreOJ】#6392. 「THUPC2018」密码学第三次小作业 / Rsa 扩展欧几里得算法
[题目]#6392. 「THUPC2018」密码学第三次小作业 / Rsa [题意]T次询问,给定正整数c1,c2,e1,e2,N,求正整数m满足: \(c_1=m^{e_1} \ \ mod \ \ ...
- 五个案例让你明白GCD死锁(转)
转自:http://ios.jobbole.com/82622/ 死锁一直都是在使用多线程时,需要注意的一个问题.以前对同步.异步,串行.并行只有一个模糊的概念,想想也是时候整理一下了.再看看之前的博 ...
- Linux - trap 命令
trap 命令用于指定在接收到信号后将要采取的动作,常见的用途是在脚本程序被中断时完成清理工作.当shell接收到sigspec指定的信号时,arg参数(命令)将会被读取,并被执行. trap 信号参 ...