系列目录

循序渐进学.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. git 创建空提交

    git commit --allow-empty -m "Empty Commit to setup deployments"

  2. 基于CMS的组件复用实践

    目前前端项目大多基于Vue.React.Angular等框架来实现,这一类框架都有一个明显的特点:基于模块化以及组件化思维.所以,开发者在使用上述框架时,实际上是在写一个一个的组件,并且组件与组件之间 ...

  3. SQL提高查询效率【in、not in、between、like】等条件讲述

    在使用SQL语句查询数据库记录时,如果要查询相同的内容,有着不同的多种方法. 仍然,尽管使用多种方法可以得到相同的结果,但是,如果您使用不同的方法,在执行效益上是截然不同的.因此,我们得仔细考虑,如果 ...

  4. 函数和常用模块【day04】: 总结(十二)

  5. 用到的设计模式总结--单例模式+工厂方法模式+Builder模式

    一,工厂方法模式和单例模式 工厂方法模式中有一个抽象的工厂接口和一个抽象的产品接口.然后,具体的工厂实现抽象工厂并负责生产具体的产品.由客户端决定 new 哪个具体的工厂,从而生产哪种产品. 因此,与 ...

  6. Java技术体系总结

    PC前端:Javascript.JQuery.Angularjs.Reactjs.TypeScript 移动前端:Vue.js.Zepto WebJars spring:spring mvc.spri ...

  7. Zookeeper笔记之四字命令

    Zookeeper支持一些命令用来获取服务的状态和相关信息,因为这些命令都是四个字母的,所以一般称为四字命令. 四字命令可以使用telnet或者nc向服务器提交,使用下面这个脚本可以当做是一个简易的客 ...

  8. ZYNQ. Interrupt(2)SPI.AXI TIMER

    Shared Peripheral Interrupts (SPI) SPI 可以接收来自PL的中断,这里使用PL模块 AXI Timer 的中断模式,并连接到CPU. AXI TIMER 定时器,内 ...

  9. OpenLayers 3 之 地图图层数据来源(ol.source)详解

    原文地址 source 是 Layer 的重要组成部分,表示图层的来源,也就是服务地址.除了在构造函数中制定外,可以使用 layer.setSource(source) 稍后指定.一.包含的类型 ol ...

  10. thymeleaf-extras-shiro

    thymeleaf-extras-shiro 转载:https://github.com/theborakompanioni/thymeleaf-extras-shiro A Thymeleaf di ...