新建完成后项目目录下有个 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. 报错:sqoop2执行job时:Exception: Job Failed with status:3

    报错背景: 创建完成sqoop2的一个job,主要功能是将数据从hdfs存到mysql数据库中. 执行job的时候发生报错. 报错现象: sqoop:> start job -j -s Subm ...

  2. [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  3. [LeetCode] 140. Word Break II 单词拆分II

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...

  4. PCL

    PCL(PointCloudLibrary)——是一个的模块化的现代C++模板库. 其基于以下第三方库:Boost.Eigen.FLANN.VTK.CUDA.OpenNI.Qhull,实现点云相关的获 ...

  5. 第7/7Beta冲刺

    1.团队成员 成员姓名 成员学号 秦裕航 201731062432(组长) 刘东 201731062227 张旭 201731062129 王伟 201731062214 2.SCRU部分 2.1各成 ...

  6. oracle11g数据库导入导出方法教程

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/xinxiaoyonng/article/ ...

  7. 1.JVM前奏篇(看官网怎么说)

    JVM(Java Virtual Machine) 前奏篇(看官网规范怎么说) 1.The relation of JDK/JRE/JVM 在下图中,我们所接触的,最熟悉,也是经常打交道的 最顶层 J ...

  8. Java找N个数中最小的K个数,PriorityQueue和Arrays.sort()两种实现方法

    最近看到了 java.util.PriorityQueue.刚看到还没什么感觉,今天突然发现他可以用来找N个数中最小的K个数. 假设有如下 10 个整数. 5 2 0 1 4 8 6 9 7 3 怎么 ...

  9. Windows 10部署教程

    1. 获取主板密钥 在powershell中执行: (Get-WmiObject -query 'select * from softwareLicensingService').OA3xOrigin ...

  10. OSGI.NET插件方式开发你的应用

    之前一直从事C# WEB开发.基本都是业务开发,性能优化. 体力活占比90%吧.模块真的很多很多,每次部署经常出先各种问题.发布经常加班. 今年开始接触winform 开发.发现C# 的事件  委托 ...