激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - Asp.net Core與類庫讀取配置文件信息的方法

Asp.net Core與類庫讀取配置文件信息的方法

2020-06-05 16:16HOYU_Z ASP.NET教程

這篇文章主要給大家介紹了關于Asp.net Core與類庫讀取配置文件信息的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言

首先開一個腦洞,Asp.net core 被使用這么長時間了,但是關于配置文件(json)的讀取,微軟官方似乎并沒有給出像.net framework讀取web.config那樣簡單且完美。嚴重懷疑這是微軟為了促進.net core 生態繁榮搞的一點小手段。

appsetting.Development.json (appsetting.json的內容和這個差不多,下面會講到多環境使用)

?
1
2
3
4
5
6
7
8
9
{
 "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的)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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中有添加

?
1
2
3
services.AddOptions();
//SettingPath極為Model
services.Configure<SettingPath>(Configuration.GetSection("SettingPath"));
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 讀取配置文件公共類

前面幾種方法之前都有用過,但是個人感覺用起來都不是很順手。而且如果想要在一個類庫中讀取配置文件的話簡直痛苦到不想理媳婦。

所以自己動手寫了一個工具類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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中添加

?
1
MyServiceProvider.ServiceProvider = app.ApplicationServices;

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

?
1
2
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
  • http://www.zmynmublwnt.cn/article/66624.html

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:http://www.cnblogs.com/hoyu/p/10026781.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久久精品网 | 国产精品久久久久久久久久久天堂 | 久草在线新视觉 | 久草在线网址 | 轻点插视频 | 欧美在线成人影院 | 欧美精品一区二区视频 | 久久久久中精品中文字幕19 | 久草影音| 国产午夜免费不卡精品理论片 | 欧美综合日韩 | 成人一级在线 | 91福利国产在线观一区二区 | 中文字幕在线永久视频 | 不卡中文一二三区 | 欧美亚洲综合网 | 久久久久成人网 | 视频www | 性盈盈盈影院 | 亚洲aⅴ免费在线观看 | 日产精品一区二区三区在线观看 | 久久久久国产一区二区三区不卡 | 精品一区二区三区在线观看视频 | 在线a亚洲视频播放在线观看 | 日韩黄色片在线观看 | 久久精品视频69 | 久久久无码精品亚洲日韩按摩 | 色网站免费观看 | 在线成人免费视频 | 久久国产精品二国产精品 | 国产va在线观看 | 一区二区三区在线观看免费视频 | 一级大片一级一大片 | 免费试看av| 国产在线地址 | 欧美男女爱爱视频 | 91精品老司机 | 91超在线 | 日本中文字幕久久 | 国产午夜精品久久久久久久蜜臀 | 日本人乱人乱亲乱色视频观看 |