.net core自定义读取配置文件
新建完成后项目目录下有个 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自定义读取配置文件的更多相关文章
- NET Core开发-读取配置文件Configuration
ASP.NET Core开发-读取配置文件Configuration ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配置系统已经和之前版本的ASP.NE ...
- .net core 学习 读取配置文件
在空项目中是没有配置文件的,首先要新建一个,配置文件内容如下,下面来读取各个内容 { "ConnectionStrings": { "DefaultConnection& ...
- ASP.NET Core开发-读取配置文件Configuration
ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配置系统已经和之前版本的ASP.NET有所不同了,之前是依赖于System.Configuration和XML ...
- ASP.NET Core开发-读取配置文件Configuration appsettings.json
https://www.cnblogs.com/linezero/p/Configuration.html ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配 ...
- asp.net core mvc 读取配置文件appsettings.json
上一篇我们将了读取自定义配置文件.这篇我们讲一下asp.net core mvc里读取自带的配置文件 appsettings.json 首先创建个asp.net core mvc项目,项目里有Prog ...
- .net core 灵活读取配置文件
using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using Syst ...
- .net core中读取配置文件
1)先看丑陋的方法 读取 appsettings.json 然后在 Startup 的 ConfigureServices() 方法中进行注入: public IConfigurationRoot ...
- 干货:.net core实现读取自定义配置文件,有源代码哦
看好多人不懂在.NET CORE中如何读取配置文件,我这里分了两篇,上一篇介绍了怎样通过appsettings.json配置读取文件信息.这一篇教大家自定义配置文件: 1.在项目下创建配置文件 { & ...
- 【无私分享:ASP.NET CORE 项目实战(第八章)】读取配置文件(二) 读取自定义配置文件
目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 我们在 读取配置文件(一) appsettings.json 中介绍了,如何读取appsettings.json. 但随之产生 ...
随机推荐
- Shell流程控制语句while
while语法格式: while 判断条件 do 命令 done while语句流程控制图: 实例: [root@youxi1 ~]# vim a.sh #!/bin/bash i=0 while [ ...
- 通过 PECL 安装 PHP 扩展(以 CentOS7 中安装 swoole 为例)
原文地址:https://blog.csdn.net/kikajack/article/details/82495190 常用工具PECL 和 phpize官网文档 PHP 有大量的扩展可以使用,比如 ...
- Python: ImportRequestsError: No module named 'requests'解决方法
运行Python程序时,出现下面错误: import requests ModuleNotFoundError: No module named ‘requests’ 原因:没有导入requests ...
- 在webstorm里使用git
1,设置git 打开webstorm软件,找到file下面的settings(设置) 打开设置对话窗,找到version control的子级目录git,路径path输入git安装目录下bin目录里的 ...
- .Net 配置 swagger 使用nginx反向代理后请求带端口号导致无法正常访问---解决方法
1 webconfig中 appsetting 中增加配置 <appSettings> <add key="aspnet:UseHostHeaderForRequestUr ...
- Java多线程-同步:synchronized 和线程通信:生产者消费者模式
大家伙周末愉快,小乐又来给大家献上技术大餐.上次是说到了Java多线程的创建和状态|乐字节,接下来,我们再来接着说Java多线程-同步:synchronized 和线程通信:生产者消费者模式. 一.同 ...
- Spring Boot 项目中的 parent
前言 我们成功创建Spring Boot之后,pom.xml坐标文件中都会有如下一段引用: <parent> <groupId>org.springframework.boot ...
- c++ 通过sizeof运算符看内存对齐
一.基础数据类型 基础数据类型的sizeof,包括char.short,int,long,float,double 注意:实际数值有所偏差,与系统相关 二.数组及字符串 包括字符数组.字符指针.字符串 ...
- PHP网文
1.php底层运行机制及原理 https://cloud.tencent.com/developer/article/1055801
- 【LEETCODE】56、数组分类,适中级别,题目:62、63、1035
package y2019.Algorithm.array.medium; /** * @ClassName UniquePathsWithObstacles * @Description TODO ...