看干货请移步至.net core 读取配置文件公共类html
首先开一个脑洞,Asp.net core 被使用这么长时间了,可是关于配置文件(json)的读取,微软官方彷佛并无给出像.net framework读取web.config那样简单且完美。严重怀疑这是微软为了促进.net core 生态繁荣搞的一点小手段。web
{ "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中有添加redis
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的状况该怎么处理。json
前面几种方法以前都有用过,可是我的感受用起来都不是很顺手。并且若是想要在一个类库中读取配置文件的话简直痛苦到不想理媳妇。app
因此本身动手写了一个工具类asp.net
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中添加ide
MyServiceProvider.ServiceProvider = app.ApplicationServices;
而后就能够在任何地方使用此类读取配置文件信息了,并且因为ConfigurationHelper初始化时已经默认加载环境变量,因此同时具有多环境功能。工具
string path = new ConfigurationHelper().config["RedisPath"]; SettingPath pathss = new ConfigurationHelper().GetAppSettings<SettingPath>("SettingPath");
参考ui
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
http://www.javashuo.com/article/p-bbrvuwkr-gu.htmlthis