一:读取配置文件

先来看一下appsettings.json文件的内容,如下图:

{
"ConnectionStrings": {
"ServerConnection": "Data Source=users.db;",
"BaseWebContext": "Server=.; database=BaseWebApp;uid=sa;pwd=@sa123;",
"RedisConnection": "127.0.0.1:6380,password=123"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Redis": {
"ConnectionString": "127.0.0.1:6380,password=123"
},
"MongoDB": {
"ConnectionString": "mongodb://127.0.0.1"
},
"AllowedHosts": "*",
"Url": "http://*:8088"
}

接下来主要是实现如何获取ConnectionStrings和Redis节点下面的链接字符串信息

1.新建ConfigurationManager.cs管理类

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks; namespace GyouApi
{
public static class ConfigurationManager
{
public readonly static IConfiguration Configuration; static ConfigurationManager()
{
Configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
} public static T GetSection<T>(string key) where T : class, new()
{
return new ServiceCollection()
.AddOptions()
.Configure<T>(Configuration.GetSection(key))
.BuildServiceProvider()
.GetService<IOptions<T>>()
.Value;
} public static string GetSection(string key)
{
return Configuration.GetValue<string>(key);
}
}
}

2.获取ConnectionStrings节点下面的配置

分别获取BaseWebContext 和RedisConnection

private static readonly string BaseWebContextString = ConfigurationManager.Configuration.GetConnectionString("BaseWebContext")

private static readonly string RedisConnectionString = ConfigurationManager.Configuration.GetConnectionString("RedisConnection");

3.获取Redis节点下面的配置

获取Redis的ConnectionString,  MongoDB下的同理


//Redis节点
private static readonly string RedisConnectionStringR = ConfigurationManager.GetSection("Redis:ConnectionString");
//ConnectionString节点
private static readonly string RedisConnectionString = ConfigurationManager.Configuration.GetConnectionString("RedisConnection");

ASP.NET Core 读取配置文件信息的更多相关文章

  1. ASP .NET CORE 读取配置文件的方法

    老式的config文件在ASP.net core2.0中变成了appsettings.json,如果想要读取自定义配置,可以写如下代码 { "Logging": { "I ...

  2. Asp.net Core 和类库读取配置文件信息

    Asp.net Core 和类库读取配置文件信息 看干货请移步至.net core 读取配置文件公共类 首先开一个脑洞,Asp.net core 被使用这么长时间了,但是关于配置文件(json)的读取 ...

  3. asp.net core 将配置文件配置迁移到数据库(一)

    asp.net core 将配置文件配置迁移到数据库(一) Intro asp.net core 配置默认是项目根目录下的 appsettings.json 文件,还有环境变量以及 command l ...

  4. ASP.NET Core的配置信息

    ASP.NET Core的配置信息 Key-Value键值对 内存里.JSON.XML.INI等文件 配置信息与配置系统是解耦的 可以依赖注入 ASP.NET Core的配置信息来源 appsetti ...

  5. Asp .Net Core 读取appsettings.json配置文件

         Asp .Net Core 如何读取appsettings.json配置文件?最近也有学习到如何读取配置文件的,主要是通过 IConfiguration,以及在Program中初始化完成的. ...

  6. asp.net core 读取Appsettings.json 配置文件

    Appsettingsjson 配置定义实体在StartUp时读取配置信息修改你的Controller通过构造函数进入配置信息总结Appsettings.json 配置很明显这个配置文件就是一个jso ...

  7. 深入探究ASP.NET Core读取Request.Body的正确方式

    前言 相信大家在使用ASP.NET Core进行开发的时候,肯定会涉及到读取Request.Body的场景,毕竟我们大部分的POST请求都是将数据存放到Http的Body当中.因为笔者日常开发所使用的 ...

  8. .NET Core 读取配置文件方式总结

    基于.NET Core的跨平台开发,配置文件与之前.NET Framework采用xml的config文件不同,目前主要是采用json文件键值对配置方式读取. 参考网上相关资料总结如下: 一.引入扩展 ...

  9. asp.net core 读取appsettings.json配置项

    1.新建一个asp.net core 项目 2.打开appsettings.json,加入配置项 { "Logging": { "IncludeScopes": ...

  10. ASP.NET Core读取AppSettings

    http://www.tuicool.com/articles/rQruMzV 今天在把之前一个ASP.NET MVC5的Demo项目重写成ASP.NET Core,发现原先我们一直用的Configu ...

随机推荐

  1. django项目初创建报错TypeError: unsupported operand type(s) for /: 'str' and 'str'

    解决办法: 'DIRS': [os.path.join(BASE_DIR, 'templates')],

  2. easyui datagrid 表头与数据错位

    方法一:容易,实用的方法 在jquery.easyui.min.js中查找到field.replace(/[\.|\s]/g, "-")在其后添加replace 例子:field. ...

  3. 使用python做基本的数据处理

    使用python做基本的数据处理 1.常用的基本数据结构 元组.列表.字典.集合.常用的序列函数 1.1基本操作 1.1.1 元组:逗号分开的序列值. tup = tuple (4,5,6) tup ...

  4. STM32F0 LL库IIC第二地址配置错误

    最近在做F0项目要用到多个IIC地址,使用Cube生成的LL库,第二地址进不了中断 F0版本:1.11.0 STM32CubeX生成的LL库代码为: 1 static void MX_I2C1_Ini ...

  5. 【转】关于 Nokogiri 的安装依赖 libxml2安装问题

    来源:https://ruby-china.org/topics/30243 在自己的os x系统上一直运行正常,包括正常使用nokogiri这个gem,今天 在本地建立新项目,bundle inst ...

  6. 设置Redhat终端显示中文

    修改.bash_profile,增加: export LC_ALL= export LANG=C source .bash_profile

  7. H5在(支付宝和微信环境)使用支付

    首先是在支付宝浏览器环境   使用支付宝支付 参考链接 https://myjsapi.alipay.com/jsapi/native/trade-pay.html 支付宝订单字符串唤起支付  调用支 ...

  8. vim中的命令行 %! 是啥意思?

    :%! command pipes the current file's contents to command's stdin, and replaces the file's contents w ...

  9. abc285h题解

    考虑容斥,强制要求\(k\)个数为完全平方数,系数为\((-1)^k*C_n^k\)(因为我们要从\(n\)个数选出\(k\)个数作为完全平方数).则在唯一分解\(p_1^{e_1}...p_n^{e ...

  10. 安卓开发学习10-1:数据存储:Shared Preferences存储

    解析 什么是Shared Perferences 应用场景 配置信息 主题信息 游戏的积分信息等 存储路径 在本应用中的data-data-应用包-自定义名称xml文件下保存写入的数据信息 使用 获取 ...