新建完成后项目目录下有个 appsettings.json

{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"SqlServer": {
"Host": "192.168.1.0",//地址
"Port": 6215,//端口号
"DataBase": "test",//连接数据库
"UserName": "sa",//用户名
"Password": "q*^fsZ#B4"//密码
},
"AllowedHosts": "*"
}
新建一个AppsettingModels类,用于获取配置数据

public class SqlServerConn
{
public string Host { get; set; }
public string Port { get; set; }
public string DataBase { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
常规的读取配置文件的数据方法

public void ConfigureServices(IServiceCollection services)
{
SqlServerConn sqlServerConn = new SqlServerConn();
Configuration.GetSection("SqlServer").Bind(sqlServerConn);
/* 另外的读取方式
var sqlServer = Configuration.GetConnectionString("SqlServer");读取节点下的所有配置
var host = Configuration["SqlServer:Host"];只读取配置里的端口
*/
//简单的配置连接的地址
services.AddDbContext<ZDDBContext>(options =>
{
options.UseLazyLoadingProxies().UseSqlServer("Server=" + sqlServerConn.Host + "," + sqlServerConn.Port + "; Database=" + sqlServerConn.DataBase + ";Persist Security Info=True;User ID=" + sqlServerConn.UserName + ";password=" + sqlServerConn.Password + ";", a => a.UseRowNumberForPaging());
}, ServiceLifetime.Scoped);
}
或者在控制器里直接注入一下

public class ValuesController : ControllerBase
{
private IConfiguration _configuration;
public ValuesController(IConfiguration configuration)
{
_configuration = configuration;
}
public IActionResult test()
{
var query = _configuration.GetSection("AllowedHosts");
return Ok();
}
}
在有的时候我们需要在别的类库里直接使用,不便于在写个构造函数来注入一下以及存在了循环引用等问题时,就可以采用自定义的模式,如下:先建SiteConfig类

/// <summary>
/// 配置信息读取模型
/// </summary>
public static class SiteConfig
{
private static IConfigurationSection _appSection = null;

/// <summary>
/// api配置信息
/// </summary>
public static string AppSetting(string key)
{
string str = string.Empty;
if (_appSection.GetSection(key) != null)
{
str = _appSection.GetSection(key).Value;
}
return str;
}

public static void SetAppSetting(IConfigurationSection section)
{
_appSection = section;
}

public static string GetSite(string apiName)
{
return AppSetting(apiName);
}
}
自定义类读取配置

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
SiteConfig.SetAppSetting(Configuration.GetSection("SqlServer"));
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseMvc();
}
使用的时候直接SiteConfig.GetSite("键");

/// <summary>
/// 读取配置
/// </summary>
/// <returns></returns>
public void ReadSite()
{
SqlConn sqlServerConn = new SqlConn();
sqlServerConn.Host = SiteConfig.GetSite("Host");
sqlServerConn.Port = SiteConfig.GetSite("Port");
sqlServerConn.DataBase = SiteConfig.GetSite("DataBase");
sqlServerConn.UserName = SiteConfig.GetSite("UserName");
sqlServerConn.Password = SiteConfig.GetSite("Password");
}
如果觉得本文有不对的地方,往大佬轻喷,告知我会立即修正。

.net core自定义读取配置文件的更多相关文章

  1. NET Core开发-读取配置文件Configuration

    ASP.NET Core开发-读取配置文件Configuration   ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配置系统已经和之前版本的ASP.NE ...

  2. .net core 学习 读取配置文件

    在空项目中是没有配置文件的,首先要新建一个,配置文件内容如下,下面来读取各个内容 { "ConnectionStrings": { "DefaultConnection& ...

  3. ASP.NET Core开发-读取配置文件Configuration

    ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配置系统已经和之前版本的ASP.NET有所不同了,之前是依赖于System.Configuration和XML ...

  4. ASP.NET Core开发-读取配置文件Configuration appsettings.json

    https://www.cnblogs.com/linezero/p/Configuration.html ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配 ...

  5. asp.net core mvc 读取配置文件appsettings.json

    上一篇我们将了读取自定义配置文件.这篇我们讲一下asp.net core mvc里读取自带的配置文件 appsettings.json 首先创建个asp.net core mvc项目,项目里有Prog ...

  6. .net core 灵活读取配置文件

    using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using Syst ...

  7. .net core中读取配置文件

    1)先看丑陋的方法 读取 appsettings.json   然后在 Startup 的 ConfigureServices() 方法中进行注入: public IConfigurationRoot ...

  8. 干货:.net core实现读取自定义配置文件,有源代码哦

    看好多人不懂在.NET CORE中如何读取配置文件,我这里分了两篇,上一篇介绍了怎样通过appsettings.json配置读取文件信息.这一篇教大家自定义配置文件: 1.在项目下创建配置文件 { & ...

  9. 【无私分享:ASP.NET CORE 项目实战(第八章)】读取配置文件(二) 读取自定义配置文件

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 我们在 读取配置文件(一) appsettings.json 中介绍了,如何读取appsettings.json. 但随之产生 ...

随机推荐

  1. plsql 记录型变量

    set serveroutput on declare emplist emp%rowtype; begin ; dbms_output.put_line(emplist.ename||'的薪水是'| ...

  2. Python - Django - 序列化

    app01/__int__.py: import pymysql pymysql.install_as_MySQLdb() app01/models.py: from django.db import ...

  3. preg_match 第三个参数,

    //请修改变量p的正则表达式,使他能够匹配str中的姓名 $p = '/name:([\w\s]+)/'; $str = "name:steven jobs"; preg_matc ...

  4. [LeetCode] 232. Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  5. [LeetCode] 582. Kill Process 终止进程

    Given n processes, each process has a unique PID (process id) and its PPID (parent process id). Each ...

  6. 利用Idea查看类的继承关系图

    1.将光标定位到你想查看的类,点击右键,选择 Diagrams,其中有 show 和 show ... Popup,只是前者新建在标签页内,后者以浮窗的形式展示 可得,如下图所示. 查看图中的Appl ...

  7. eoj monthly 2019.11

    原题 T1 纸条 题目大意: 给出一个长度为n的字符串,其中m位未知,对于每一位未知的字母,有k个备选字母,最终答案为备选字母按字典序排序后的第x个. 题解: 签到题-- 按照题目意思直接写就可以了. ...

  8. 线性DP详解

    顾名思义,线性DP就是在一条线上进行DP,这里举一些典型的例子. LIS问题(最长上升子序列问题) 题目 给定一个长度为N的序列A,求最长的数值单调递增的子序列的长度. 上升子序列B可表示为B={Ak ...

  9. Django——用户认证

    Django--用户认证 用户与Authentication(身份验证) Django 用户认证系统处理用户帐号,组,权限以及基于cookie的用户会话. 这个系统一般被称为 auth/auth (认 ...

  10. openstack-nova源码之阅读流程

    以创建虚拟机为例 1.项目入口setup.cfg文件 2.根据nova-compute = nova.cmd.compute:main找到功能入口 3.nova/api/openstack/compu ...