获取ConnectionStrings节点数据

//appsettings.json

{
"ConnectionStrings": {
//DEV
"DbConn": "Server=**;Integrated Security=no;User ID=**;PWD=**;initial catalog=DB**;MultipleActiveResultSets=true;Max Pool Size=1024;Min Pool Size=10;Pooling=true;"
//QA
//PROD
},
"ErrorPage": "/Error/Error",
"Environment": "DEV"
}
//startup.cs

public Startup(IConfiguration configuration) //依赖注入
{
_configuration = configuration;
} public IConfiguration _configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
string _conn;
//config db
try
{
//GetSection("Environment")获取Environment节点的数据
if (_configuration.GetSection("Environment").Value.Equals("PROD", StringComparison.OrdinalIgnoreCase))
{
var service = new DecryptService("ProjectICE_Portal");
_conn = service.Decrypt(_configuration.GetConnectionString("DbConn"));//加密获取节点数据
}
else
{
_conn = _configuration.GetConnectionString("DbConn");//非加密
}
}
catch (Exception ex)
{
throw new Exception($"Database connection initialization failed: {ex.Message}{ex.StackTrace}");
}
}

第二种比较实用的方法

//Appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning"
}
},
"AllowedHosts": "*",
"student": {
"name": "小明",
"age": 17,
"classname": "5班" }
} //新建一个实体类 实体类的属性和配置文件的配置项一致
public class student
{
public string name { get; set; }
public int age { get; set; }
// public string classname { get; set; }
} //Startup.cs //services.AddOptions(); 这两个必须在AddMvc上面
services.Configure<student>(Configuration.GetSection("student"));
services.AddMvc();

//Controller 依赖注入 using Microsoft.Extensions.Options;
public class OneController : Controller
{
private readonly IOptions<student> _log;
public OneController(IOptions<student> logs)
{
_log = logs;
}
public IActionResult Index()
{
var a = _log.Value;
ViewBag.a = a.name; //"小明"
return View();
}
}

.Net Core中获取appsettings.json中的节点数据的更多相关文章

  1. .NET Core类库项目中如何读取appsettings.json中的配置

    这是一位朋友问我的问题,写篇随笔回答一下.有2种方法,一种叫丑陋的方法 —— IConfiguration ,一种叫优雅的方法 —— IOptions . 1)先看丑陋的方法 比如在 RedisCli ...

  2. .Net Core 读取配置文件 appsettings.json

    1. 首先些一个类 public class MySettings { public string P1 { get; set; } public string P2 { get; set; } } ...

  3. 在swt中获取jar包中的文件 uri is not hierarchical

    uri is not hierarchical 学习了:http://blog.csdn.net/zdsdiablo/article/details/1519719 在swt中获取jar包中的文件: ...

  4. 【记录】mybatis中获取常量类中数据

    部分转载,已注明来源: 1.mybatis中获取常量类中数据 <update id="refuseDebt"> UPDATE dt_debt a SET         ...

  5. .NET Core 类库中读取appsettings.json

    { "Logging": { "IncludeScopes": false, "LogLevel": { "Default&quo ...

  6. .NET Core 中读取appsettings.json配置文件的方法

    appsettings.json配置文件结构如下: { "WeChatPay": { "WeChatApp_ID": "wx9999998999&qu ...

  7. 如何在.Net Core 2.0 App中读取appsettings.json

    This is something that strangely doesn’t seem to be that well documented and took me a while to figu ...

  8. .Net Core 2.0 App中读取appsettings.json

    引用: Microsoft.Extensions.ConfigurationMicrosoft.Extensions.Configuration.FileExtensionsMicrosoft.Ext ...

  9. Asp.Net Core探索 之 appsettings.json

    appsettings.json是什么? 相信大家在.Net Framework的项目都会用的web.config,app.config这些文件,appsettings.json文件就是Asp.Net ...

随机推荐

  1. Spring源码 02 项目搭建

    参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...

  2. React报错之Type '() => JSX.Element[]' is not assignable to type FunctionComponent

    正文从这开始~ 总览 当我们尝试从函数组件中返回元素组成的数组时,会产生"Type '() => JSX.Element[]' is not assignable to type Fu ...

  3. 刷题记录:Codeforces Round #731 (Div. 3)

    Codeforces Round #731 (Div. 3) 20210803.网址:https://codeforces.com/contest/1547. 感觉这次犯的低级错误有亿点多-- A 一 ...

  4. [CSP day1T3]树上的数

    题面 题解 这道题由于是求字典序最小的,所以要贪心地枚举数字,然后找可以走到的编号最小的点,处理这条路径. 这条路径有一些特性. 以下是特别精炼的结论: 所以一旦选好了路径,这些边的先后顺序就被定死了 ...

  5. JZOJ 5796 划分 (容斥,数论,扩展CRT)

    题面 有一个未知的序列 x,长度为 n.它的 K-划分序列 y 指的是每连续 K 个数的和得到划 分序列,y[1]=x[1]+x[2]+....+x[K],y[2]=x[K+1]+x[K+2]+... ...

  6. 03_Django-GET请求和POST请求-设计模式及模板层

    03_Django-GET请求和POST请求-设计模式及模板层 视频:https://www.bilibili.com/video/BV1vK4y1o7jH 博客:https://blog.csdn. ...

  7. 总在用户态调试 C# 程序,终还是搭了一个内核态环境

    一:背景 一直在用 WinDbg 调试用户态程序,并没有用它调试过 内核态,毕竟不是做驱动开发,也没有在分析 dump 中需要接触用内核态的需求,但未知的事情总觉得很酷,加上最近在看 <深入解析 ...

  8. 员工离职困扰?来看AI如何解决,基于人力资源分析的 ML 模型构建全方案 ⛵

    作者:韩信子@ShowMeAI 数据分析实战系列:https://www.showmeai.tech/tutorials/40 机器学习实战系列:https://www.showmeai.tech/t ...

  9. Oracle与KingbaseES的NULL在索引使用上的区别

    NULL值是关系型数据库系统中比较特殊的一种值,通常称为UNKNOWN或空值,即是未知的,不确定的.由于NULL存在着无数的可能,因此NULL值也不等于NULL值. Oracle在创建索引时,不会存储 ...

  10. KingbaseES R6 集群sys_monitor.sh change_password一键修改集群用户密码

    案例说明: kingbaseES R6集群用户密码修改,需要修改两处: 1)修改数据库用户密码(alter user): 2)修改.encpwd文件中用户密码: 可以通过sys_monitor.sh ...