如何在Asp.net Core中读取配置文件信息

发布时间:2021-05-24 16:53:09 作者:Leah
来源:亿速云 阅读:122

如何在Asp.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 读取配置文件的博客,感觉都没有很好的解决问题。

第一种是在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");

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. ASP.NET Core实现类库项目读取配置文件
  2. ASP.NET Core类库项目中实现读取配置文件的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

core

上一篇:如何在asp.net Core3.0中配置区域与路由

下一篇:怎么在.NET Core中利用基于Generic Host实现后台任务

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》