Asp.net Core与类库读取配置文件信息的方法

2025-05-29 0 29

前言

首先开一个脑洞,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.zzvips.com/article/66624.html

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对快网idc的支持。

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Asp.net Core与类库读取配置文件信息的方法 https://www.kuaiidc.com/98491.html

相关文章

发表评论
暂无评论