ASP.NET Core 读取配置文件信息
一:读取配置文件
先来看一下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 读取配置文件信息的更多相关文章
- ASP .NET CORE 读取配置文件的方法
老式的config文件在ASP.net core2.0中变成了appsettings.json,如果想要读取自定义配置,可以写如下代码 { "Logging": { "I ...
- Asp.net Core 和类库读取配置文件信息
Asp.net Core 和类库读取配置文件信息 看干货请移步至.net core 读取配置文件公共类 首先开一个脑洞,Asp.net core 被使用这么长时间了,但是关于配置文件(json)的读取 ...
- asp.net core 将配置文件配置迁移到数据库(一)
asp.net core 将配置文件配置迁移到数据库(一) Intro asp.net core 配置默认是项目根目录下的 appsettings.json 文件,还有环境变量以及 command l ...
- ASP.NET Core的配置信息
ASP.NET Core的配置信息 Key-Value键值对 内存里.JSON.XML.INI等文件 配置信息与配置系统是解耦的 可以依赖注入 ASP.NET Core的配置信息来源 appsetti ...
- Asp .Net Core 读取appsettings.json配置文件
Asp .Net Core 如何读取appsettings.json配置文件?最近也有学习到如何读取配置文件的,主要是通过 IConfiguration,以及在Program中初始化完成的. ...
- asp.net core 读取Appsettings.json 配置文件
Appsettingsjson 配置定义实体在StartUp时读取配置信息修改你的Controller通过构造函数进入配置信息总结Appsettings.json 配置很明显这个配置文件就是一个jso ...
- 深入探究ASP.NET Core读取Request.Body的正确方式
前言 相信大家在使用ASP.NET Core进行开发的时候,肯定会涉及到读取Request.Body的场景,毕竟我们大部分的POST请求都是将数据存放到Http的Body当中.因为笔者日常开发所使用的 ...
- .NET Core 读取配置文件方式总结
基于.NET Core的跨平台开发,配置文件与之前.NET Framework采用xml的config文件不同,目前主要是采用json文件键值对配置方式读取. 参考网上相关资料总结如下: 一.引入扩展 ...
- asp.net core 读取appsettings.json配置项
1.新建一个asp.net core 项目 2.打开appsettings.json,加入配置项 { "Logging": { "IncludeScopes": ...
- ASP.NET Core读取AppSettings
http://www.tuicool.com/articles/rQruMzV 今天在把之前一个ASP.NET MVC5的Demo项目重写成ASP.NET Core,发现原先我们一直用的Configu ...
随机推荐
- C#中socket的简单使用
一.Socket的概念Socket其实并不是一个协议,而是为了方便使用TCP或UDP而抽象出来的一层,是位于应用层和传输控制层之间的一组接口. 当两台主机通信是,必须通过Socket连接,Socket ...
- php8.0.0新功能:Match表达式
Match表达式是基于值的一致性进行分支计算.它的比较是严格比较.Match表达式从php8.0.0起可用.示例代码: 1 $key = 'key_1'; 2 $value = match($key) ...
- 解决 http://www.diamond-sh.com/favicon.ico 404 (Not Found) 报错问题
html5页面中经常会遇到这个报错,解决方法有以下两种: 1. 根目录下建一个个favicon.ico文件,在head标签引入favicon.ico文件即可 <link href="f ...
- Oracle.DataAccess使用问题汇总
1.使用参数化传参 先看一段sql select TABLE_COLUMN_NAME from CSV_PARA_MAPPING where TABLE_NAME = ':v_tabName' and ...
- ubuntu18 build opencv4 from source
1 安装依赖 ## Install dependencies sudo apt -y install build-essential checkinstall cmake pkg-config yas ...
- JavaScript严格模式(use strict)
一.什么是严格模式(strict mode) JavaScript严格模式即在严格模式下运行.严格模式下,你将不能使用未声明的变量. 注意,严格模式需要浏览器的支持:Internet explorer ...
- MogDB 学习笔记之 -- PITR恢复
# 概念描述## 背景信息当数据库崩溃或希望回退到数据库之前的某一状态时,MogDB的即时恢复功能(Point-In-Time Recovery,简称PITR)可以支持恢复到备份归档数据之后的任意时间 ...
- [JSOI2015]圈地
原题链接:P6094 [JSOI2015]圈地 题意简述 把一块 \(n \times m\) 的地分给两个人,选择分出第 \(i\) 行第 \(j\) 列的地可以获得 \(a_{i,j}\) 的收益 ...
- Python接口测试request
requests安装 用pip安装requests模块 pip install requests 一.Get的使用 格式:get(url, params=None, **kwargs) Get常见查询 ...
- Python自动化测试更新selenium的两种方式
第一种手动实现: 来源 https://www.codeleading.com/article/73395290617/ import os import re import winreg impor ...