plu小小海贼王,北京seo邢云涛,燕山大学论坛
看干货请移步至
首先开一个脑洞,asp.net core 被使用这么长时间了,但是关于配置文件(json)的读取,微软官方似乎并没有给出像.net framework读取web.config那样简单且完美。严重怀疑这是微软为了促进.net core 生态繁荣搞的一点小手段。
{ "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" }
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"]; } }
这种方法需要在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的情况该怎么处理。
前面几种方法之前都有用过,但是个人感觉用起来都不是很顺手。而且如果想要在一个类库中读取配置文件的话简直痛苦到不想理媳妇。
所以自己动手写了一个工具类
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://www.cnblogs.com/createmyself/p/6859076.html
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Net Core Web Api项目与在NginX下发布的方法
asp.net core3.1 引用的元包dll版本兼容性问题解决方案
IdentityServer4实现.Net Core API接口权限认证(快速入门)
ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径的实现
网友评论