系列目录

循序渐进学.Net Core Web Api开发系列目录

本系列涉及到的源码下载地址: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的更多相关文章

  1. 循序渐进学.Net Core Web Api开发系列【0】:序言与目录

    一.序言 我大约在2003年时候开始接触到.NET,最初在.NET framework 1.1版本下写过代码,曾经做过WinForm和ASP.NET开发.大约在2010年的时候转型JAVA环境,这么多 ...

  2. 循序渐进学.Net Core Web Api开发系列【16】:应用安全续-加密与解密

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 应用安全除 ...

  3. 循序渐进学.Net Core Web Api开发系列【15】:应用安全

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍W ...

  4. 循序渐进学.Net Core Web Api开发系列【14】:异常处理

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍异 ...

  5. 循序渐进学.Net Core Web Api开发系列【13】:中间件(Middleware)

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍如 ...

  6. 循序渐进学.Net Core Web Api开发系列【12】:缓存

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍如 ...

  7. 循序渐进学.Net Core Web Api开发系列【11】:依赖注入

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍如 ...

  8. 循序渐进学.Net Core Web Api开发系列【10】:使用日志

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.本篇概述 本篇介 ...

  9. 循序渐进学.Net Core Web Api开发系列【9】:常用的数据库操作

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇描述一 ...

  10. 循序渐进学.Net Core Web Api开发系列【8】:访问数据库(基本功能)

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇讨论如 ...

随机推荐

  1. Hadoop生态圈-Flume的组件之自定义Sink

    Hadoop生态圈-Flume的组件之自定义Sink 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客主要介绍sink相关的API使用两个小案例,想要了解更多关于API的小技 ...

  2. git高清技能图

    ----

  3. bzoj千题计划241:bzoj3864: Hero meet devil

    http://www.lydsy.com/JudgeOnline/problem.php?id=3864 题意: 给你一个DNA序列,求有多少个长度为m的DNA序列和给定序列的LCS为0,1,2... ...

  4. bzoj千题计划208:bzoj3174: [Tjoi2013]拯救小矮人

    http://www.lydsy.com/JudgeOnline/problem.php?id=3174 按a+b从小到大排序,a+b小的在上面,先考虑让它逃出去 正确性不会证 感性理解一下,最后一个 ...

  5. 一个ssm综合小案例-商品订单管理-第一天

    项目需求分析: 功能需求:登录,商品列表查询,修改 项目环境及技术栈: 项目构成及环境: 本项目采用 maven 构建 环境要求: IDEA Version: 2017.2.5 Tomcat Vers ...

  6. dedecms织梦首页判断,添加不同标题

    <title> {dede:field.title/} {dede:field name='typeid' runphp="yes"}(@me==0)? @me=&qu ...

  7. myBatis 3.2.7 如何打印 sql

    该文中使用的log框架为logback myBatis3.0.6左右的版本时 打印sql的时候只需要配置如下属性: <logger name="java.sql.Connection& ...

  8. python 喜马拉雅 音乐下载 演示代码

    1.主程序文件 import os import json import requests from contextlib import closing from progressbar import ...

  9. HDU 1863 畅通工程 最下生成树问题

    题目描述:给出图,要你求是否存在最小生成树,如果存在,要求输出最小权值和,如果不存在,输出? 解题报告:又是一个最裸的克鲁斯卡尔,并且要判断是否存在最小生成树的问题.废话不多说,给个短代码: #inc ...

  10. ViewGroup.layout(int l, int t, int r, int b)四个输入参数的含义

    ViewGroup.layout(int l, int t, int r, int b)这个方法是确定View的大小和位置的,然后将其绘制出来,里面的四个参数分别是View的四个点的坐标,他的坐标不是 ...