午夜无码人妻aⅴ大片色欲张津瑜,国产69久久久欧美黑人A片,色妺妺视频网,久久久久国产综合AV天堂

如何在Asp.netCore中讀取配置文件信息-創(chuàng)新互聯(lián)

如何在Asp.net Core中讀取配置文件信息?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)自2013年創(chuàng)立以來(lái),是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元賓陽(yáng)做網(wǎng)站,已為上家服務(wù),為賓陽(yáng)各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792

首先開一個(gè)腦洞,Asp.net core 被使用這么長(zhǎng)時(shí)間了,但是關(guān)于配置文件(json)的讀取,微軟官方似乎并沒有給出像.net framework讀取web.config那樣簡(jiǎn)單且完美。嚴(yán)重懷疑這是微軟為了促進(jìn).net core 生態(tài)繁榮搞的一點(diǎn)小手段。

appsetting.Development.json (appsetting.json的內(nèi)容和這個(gè)差不多,下面會(huì)講到多環(huán)境使用)


{
 "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 讀取配置文件的博客,感覺都沒有很好的解決問(wèn)題。


  • 最簡(jiǎn)單的就是在StartUp中通過(guò)Configuration["SettingPath:VirtualPath"]的形式獲取信息;

  • 接下來(lái)就是在Controller中獲去配置文件信息,在控制器中讀取配置文件有兩種方法。

第一種是在controller初始化的時(shí)候把IHostingEnvironment,IConfiguration傳過(guò)來(lái),然后把穿過(guò)來(lái)的值賦給controller中對(duì)應(yīng)的變量,酒后就可以正常讀取配置文件了(由于我是個(gè)菜逼,還沒看明白系統(tǒng)啟動(dòng)的時(shí)候,這兩個(gè)變量是怎么傳給controller的)


  public class HomeController : Controller
 {
  //環(huán)境變量
  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"];
  }
 }

第二種是通過(guò)獲取對(duì)象的方式讀取配置文件,最近很多博客說(shuō)的都是關(guān)于這個(gè)的。還是在controller初始化的時(shí)候把IOptions傳進(jìn)來(lái)(這里我還是沒懂怎么傳過(guò)來(lái)的/(ㄒoㄒ)/~~),然后把傳過(guò)來(lái)的值賦值給Model的對(duì)象,然后就可以正常使用了。


這種方法需要在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
  }
 }

這里因?yàn)槲也涣私?,IOptions是怎么傳進(jìn)來(lái)的,所以不知道如果有需要只用兩個(gè)或以上Model的情況該怎么處理。

.net core 讀取配置文件公共類


前面幾種方法之前都有用過(guò),但是個(gè)人感覺用起來(lái)都不是很順手。而且如果想要在一個(gè)類庫(kù)中讀取配置文件的話簡(jiǎn)直痛苦到不想理媳婦。

所以自己動(dòng)手寫了一個(gè)工具類

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;
  }
 }
 //我比較喜歡單獨(dú)放這個(gè)類,但是這樣放更明顯
 public class MyServiceProvider
 {
  public static IServiceProvider ServiceProvider { get; set; }
 }
}

使用這個(gè)類的話需要在StartUp的Configure中添加

 MyServiceProvider.ServiceProvider = app.ApplicationServices;

然后就可以在任何地方使用此類讀取配置文件信息了,而且由于ConfigurationHelper初始化時(shí)已經(jīng)默認(rèn)加載環(huán)境變量,所以同時(shí)具備多環(huán)境功能。

 string path = new ConfigurationHelper().config["RedisPath"];
   SettingPath pathss = new ConfigurationHelper().GetAppSettings<SettingPath>("SettingPath");

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。

分享題目:如何在Asp.netCore中讀取配置文件信息-創(chuàng)新互聯(lián)
本文URL:http://www.ekvhdxd.cn/article4/cdciie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、Google網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司商城網(wǎng)站、營(yíng)銷型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司