获取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. 巨细靡遗流程控制,Go lang1.18入门精炼教程,由白丁入鸿儒,Go lang流程结构详解EP09

    流程结构就是指程序逻辑到底怎么执行,进而言之,程序执行逻辑的顺序.众所周知,程序整体都是自上由下执行的,但有的时候,又不仅仅是从上往下执行那么简单,大体上,Go lang程序的流程控制结构一共有三种: ...

  2. 【Manim CE】常用Mobject

    当前文档版本:v0.16.0.post0 VMobject 继承自Mobject V的意思是向量化的,vectorized mobject fill_color=None, fill_opacity= ...

  3. 在 node 中使用 jquery ajax

    对于前端同学来说,ajax 请求应该不会陌生.jquery 真的ajax请求做了封装,可以通过下面的方式发送一个请求并获取相应结果: $.ajax({ url: "https://echo. ...

  4. awk5个使用场景

    awk简介 首先要知道awk的使用场景,需了解awk有哪些优势与短板. 关于个人近期学习awk总结其优势: awk对文本的处理运算效率同比其他工具效率高很多(比shell的for循环高10倍以上,运算 ...

  5. 新零售SaaS架构:商品系统架构设计

    SaaS产品就像一座冰山,冰山以上的部分是功能.数据(可见部分).用户界面,冰山以下是系统架构.完整的数据模型.开放体系.非功能性需求(扩展性.可维护性.性能.安全等). 短期内想要快速上线产品,可能 ...

  6. BI系统的分布式部署原理和技术实现

    1.什么是分布式 关于"分布式系统"的定义,我们先看下书中是怎么说的.<分布式系统原理和范型>一书中是这样定义分布式系统的:"分布式系统是若干独立计算机的集合 ...

  7. K8S name_namespace

    Name 由于K8S内部,使用"资源"来定义每一种逻辑概念(功能),故没种"资源",都应该有自己的"名称" "资源"有 ...

  8. Elasticsearch7.6.2 RestHighLevelClient查询用法 must should(and or 关系)

    1. 引入jar <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId&g ...

  9. Typora Markdown 安装包

    下载地址: 链接:https://pan.baidu.com/s/1wy0Ik95AjM5WjSC3nzOzqA 提取码:f26j 复制这段内容后打开百度网盘手机App,操作更方便哦 已更新至最新版0 ...

  10. LVGL 模拟仿真(Windows+CodeBlocks)

    一.准备材料 Code Blocks官网:https://www.codeblocks.org/ Code Blocks 汉化包:链接: https://pan.baidu.com/s/12zB5bD ...