Asp.net Core 和类库读取配置文件信息
Asp.net Core 和类库读取配置文件信息
看干货请移步至.net core 读取配置文件公共类
首先开一个脑洞,Asp.net core 被使用这么长时间了,但是关于配置文件(json)的读取,微软官方似乎并没有给出像.net framework读取web.config那样简单且完美。严重怀疑这是微软为了促进.net core 生态繁荣搞的一点小手段。
appsetting.Development.json (appsetting.json的内容和这个差不多,下面会讲到多环境使用)
{
"SettingPath": {
"VideoFilePath": "C:\\Users\\89275\\Desktop\\Projects\\mv",
"FfmpegPath": "C:/Users/89275/Desktop/Projects/mv/ffmpeg.exe",
"FtpPath": "http://192.168.254.1/videofile",
"VirtualPath": "/videoplay"
},
"RedisPath":"192.168.0.108:6379"
}
看了很多Asp.net core 读取配置文件的博客,感觉都没有很好的解决问题。
- 最简单的就是在StartUp中通过Configuration["SettingPath:VirtualPath"]的形式获取信息;
- 接下来就是在Controller中获去配置文件信息,在控制器中读取配置文件有两种方法。
- 第一种是在controller初始化的时候把IHostingEnvironment,IConfiguration传过来,然后把穿过来的值赋给controller中对应的变量,酒后就可以正常读取配置文件了(由于我是个菜逼,还没看明白系统启动的时候,这两个变量是怎么传给controller的)
public class HomeController : Controller
{
//环境变量
private readonly IHostingEnvironment hostingEnvironment;
private IConfiguration Configuration;
public HomeController(IHostingEnvironment hostingEnvironment, IConfiguration configuration)
{
this.hostingEnvironment = hostingEnvironment;
Configuration = configuration;
}
pubilc void GetRedisPath()
{
string redisPath = Configuration["RedisPath"];
}
}
- 第二种是通过获取对象的方式读取配置文件,最近很多博客说的都是关于这个的。还是在controller初始化的时候把IOptions传进来(这里我还是没懂怎么传过来的/(ㄒoㄒ)/~~),然后把传过来的值赋值给Model的对象,然后就可以正常使用了。
这种方法需要在StartUp中的ConfigureServices中有添加
services.AddOptions();
//SettingPath极为Model
services.Configure<SettingPath>(Configuration.GetSection("SettingPath"));
public class HomeController
{
public SettingPath settingPath;
private ILog log = LogManager.GetLogger(Startup.repository.Name, typeof(VideosController));
public HomeController(IOptions<SettingPath> option)
{
settingPath = option.Value;
}
public void GetVideoPath()
{
string path=SettingPath.VideoFilePath
}
}
这里因为我不了解,IOptions是怎么传进来的,所以不知道如果有需要只用两个或以上Model的情况该怎么处理。
.net core 读取配置文件公共类
前面几种方法之前都有用过,但是个人感觉用起来都不是很顺手。而且如果想要在一个类库中读取配置文件的话简直痛苦到不想理媳妇。
所以自己动手写了一个工具类
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System;
namespace Common
{
public class ConfigurationHelper
{
public IConfiguration config { get; set; }
public ConfigurationHelper()
{
IHostingEnvironment env = MyServiceProvider.ServiceProvider.GetRequiredService<IHostingEnvironment>();
config = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables()
.Build();
}
public T GetAppSettings<T>(string key) where T : class, new()
{
var appconfig = new ServiceCollection()
.AddOptions()
.Configure<T>(config.GetSection(key))
.BuildServiceProvider()
.GetService<IOptions<T>>()
.Value;
return appconfig;
}
}
//我比较喜欢单独放这个类,但是这样放更明显
public class MyServiceProvider
{
public static IServiceProvider ServiceProvider { get; set; }
}
}
使用这个类的话需要在StartUp的Configure中添加
MyServiceProvider.ServiceProvider = app.ApplicationServices;
然后就可以在任何地方使用此类读取配置文件信息了,而且由于ConfigurationHelper初始化时已经默认加载环境变量,所以同时具备多环境功能。
string path = new ConfigurationHelper().config["RedisPath"];
SettingPath pathss = new ConfigurationHelper().GetAppSettings<SettingPath>("SettingPath");
参考
https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.1
https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/environments?view=aspnetcore-2.1
https://www.cnblogs.com/CreateMyself/p/6859076.html
Asp.net Core 和类库读取配置文件信息的更多相关文章
- ASP.NET Core系列:读取配置文件
1. 控制台应用 新建一个控制台应用,添加两个Package: Install-Package Microsoft.Extensions.Configuration Install-Package M ...
- .net core mvc 类库读取配置文件
appsettings.json,给类库项目引入 Microsoft.Extensions.Configuration 和 Microsoft.Extensions.Configuration.J ...
- ASP.NET Core实现类库项目读取配置文件
前言 之前继续在学习多线程方面的知识,忽然这两天看到博问中有个园友问到如何在.net core类库中读取配置文件,当时一下蒙了,这个提的多好,我居然不知道,于是这两天了解了相关内容才有此篇博客的出现, ...
- ASP.Net Core 5.0 MVC 配置文件读取,Startup 类中ConfigureServices 方法、Configure 方法的使用
配置文件读取 1. 新建FirstController控制器 在appsettings文件内容替换成以下代码 { "Position": { "Title": ...
- .net core 2.0 读取配置文件
1.引用Microsoft.Extensions.Configuration2.在Startup中注入服务 public static IConfiguration Configuration { g ...
- ConfigParser_读取配置文件信息
ConfigParse简介 ConfigParser 在python中是用来解析配置文件的内置模块,直接导入使用 import configparser 使用该模块可以对配置文件进行增.读.改.删操作 ...
- 在java中读取配置文件信息
public class PropertyUtil { public static final Properties PROP = new Properties(); /** * 读取配置文件的内容( ...
- ASP.NET CORE入门之读取Json配置文件
首先新建一.net core控制台项目,命名为jsonReader 然后选中引用,选择NuGet包管理器,点击浏览引入mircosoft.aspnetcore.all并安装 选中解决方案,填加,新建项 ...
- ASP.NET、WinForm、C# - 配置文件信息读取 [ Web.config || Appconfig ]
<configuration> <appSettings> <add key="name" value="HF_Ultrastrong&qu ...
随机推荐
- jq闭包
var jy = jQuery.noConflict(); (function($){ //里面跟jq的所有代码 })(jy)
- suse安装gcc,升级到4.8.5
前面这些是挂载iso,如果iso可以使用,就不需要下面几步. cd /etc/zypp/repos.d mkdir iso chmod -R 777 iso mount -o loop /media/ ...
- 37. Sudoku Solver (Array;Back-Track)
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- iOS 各种方法
tableViewCell分割线左对齐: - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)c ...
- 编辑距离12 · Edit Distance12
[抄题]: 给出两个单词word1和word2,计算出将word1 转换为word2的最少操作次数. 你总共三种操作方法: 插入一个字符 删除一个字符 替换一个字符 [思维问题]: [一句话思路]: ...
- yaf框架安装配置
YAF中文文档:http://www.laruence.com/manual/index.html 1 YAF框架是用C开发的,属于PHP的扩展框架: 2 YAF的性能相对于源生PHP,性能只降低不到 ...
- MySQL 开启远程访问权限
1.登陆mysql数据库 mysql -u root -p 查看user表 mysql> use mysql;Database changedmysql> select host,u ...
- UI设计:掌握这6点,轻松0到1
非科班出身能成为UI设计师吗? 答案是肯定的.世上无难事,只怕有心人.只要找对方法.坚持不懈,即便是零基础也能学好UI设计. 那么零基础学习UI设计,需要学习哪些知识?我们要从哪些地方学起?怎么系统学 ...
- python之web开发利器
http://docs.jinkan.org/docs/flask/ https://www.djangoproject.com/
- 树结构(三)----平衡二叉树(AVL树)
将二叉排序树的的缺点优化,继承二叉排序的树的优化 左子树和右子树的高度差的绝对值不超过1