.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");
}
如果觉得本文有不对的地方,往大佬轻喷,告知我会立即修正。
相关文章
- C#.NET常见问题(FAQ)-控制台程序如何做弹窗
- C#.NET常见问题(FAQ)-在VS程序如何取消.vshost的进程
- ASP.NET Core 中的筛选器
- 重新整理 .net core 实践篇—————3种配置验证[十四]
- 重新整理 .net core 实践篇—————配置系统之强类型配置[十]
- 《ASP.NET Core 6框架揭秘》目录
- ASP.NET Core 6框架揭秘实例演示[09]:将配置绑定为对象
- [ASP.NET Core 3框架揭秘]服务承载系统[3]:总体设计[上篇]
- [ASP.NET Core 3框架揭秘] 文件系统[3]:物理文件系统
- [ASP.NET Core 3框架揭秘] 依赖注入[1]:控制反转
- ASP.NET Core应用的错误处理[3]:ExceptionHandlerMiddleware中间件如何呈现“定制化错误页面”
- ASP.NET Core的配置(5):配置的同步[ 实例篇]
- ASP.NET Core的配置(3): 将配置绑定为对象[上篇]
- ASP.NET Core中的依赖注入(5): ServiceProvider实现揭秘 【总体设计 】
- 【开源】C#.NET股票历史数据采集,【附18年历史数据和源代码】
- Atitit ..Net Framework sdk 3.0 3.5 4.04.5 4.6 4.7 .net core版本新特性 v2 s22 1. 新特性来源于 down ms 官方网站1
- npm——安装教程、安装vue脚手架(ASP.NET Core微服务(五)——【vue脚手架解析接口】过度章节)
- openpose_net随机搜索(维度搜索)网络源代码
- 用ASP.NET Core MVC 和 EF Core 构建Web应用 (一)
- asp.net core 过滤器
- asp.net core 知识点总结
- net core HTTP Error 500.31 - Failed to load ASP.NET Core runtime HTTP Error 500.30