.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. 但随之产生 ...
随机推荐
- Mysql查询的执行顺序
顺序: FROM JOIN ON WHERE GROUP BY (从此处开始可以使用select中指定的别名) AVG.SUM.MAX HAVING SELECT DISTINCT ORDER BY ...
- CentOS7 安装特定版本的Docker
先卸载旧版本 sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-late ...
- iptables的statistic模块配置rr
网上搜的是这样的 : iptables -A PREROUTING -t nat -d 172.17.64.8 -m statistic --mode nth --every 2 --packet 0 ...
- [LeetCode] 257. Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- sshpass命令使用
1.直接远程连接某主机 sshpass -p {密码} ssh {用户名}@{主机IP} 2.远程连接指定ssh的端口 sshpass -p {密码} ssh -p ${端口} {用户名}@{主机IP ...
- SpringBoot+Mybatis+MySql 自动生成代码 自动分页
一.配置文件 <!-- 通用mapper --> <dependency> <groupId>tk.mybatis</groupId> <arti ...
- string 与 int double 的转化
#include <iostream> #include <string> #include <sstream> using namespace std; int ...
- 【LeetCode】删除排序数组中的重复项&&移除特定元素【双指针,原地算法】
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1 ...
- 【LEETCODE】57、数组分类,适中级别,题目:969、442、695
package y2019.Algorithm.array.medium; import java.util.ArrayList; import java.util.List; /** * @Proj ...
- idea 代码热启动配置方法
Restart Server:代码已更新重启tomcat服务器