Preface
First of all, I have a brainstorm. Core has been used for so long, but regarding the reading of configuration files (json), Microsoft does not seem to have given it as simple and perfect as .net framework reading. I seriously suspect that this is a small trick Microsoft has done to promote the prosperity of the .net core ecosystem.
(The content is similar to this one, and the following will talk about using multiple environments)
{ "SettingPath": { "VideoFilePath": "C:\\Users\\89275\\Desktop\\Projects\\mv", "FfmpegPath": "C:/Users/89275/Desktop/Projects/mv/", "FtpPath": "http://192.168.254.1/videofile", "VirtualPath": "/videoplay" }, "RedisPath":"192.168.0.108:6379" }
I have read a lot of blogs that read core configuration files, but I feel that it has not solved the problem well.
- The easiest thing is to obtain information in StartUp through the form of Configuration["SettingPath:VirtualPath"];
- Next is to obtain configuration file information in the Controller. There are two ways to read configuration files in the controller.
The first is to pass the IHostingEnvironment and IConfiguration when the controller is initialized, and then assign the value passed to the corresponding variables in the controller. You can read the configuration file normally after drinking (because I am a slut, and I haven't understood how these two variables are passed to the controller when the system is started)
public class HomeController : Controller { //Environment variables private readonly IHostingEnvironment hostingEnvironment; private IConfiguration Configuration; public HomeController(IHostingEnvironment hostingEnvironment, IConfiguration configuration) { = hostingEnvironment; Configuration = configuration; } pubilc void GetRedisPath() { string redisPath = Configuration["RedisPath"]; } }
The second type is to read the configuration file by obtaining objects. Recently, many blogs have been talking about this. It is still possible to pass the IOptions in when the controller is initialized (I still don’t know how to pass it here /(ㄒoㄒ)/~~), and then assign the passed value to the Model object, and then it can be used normally.
This method needs to be added in ConfigureServices in StartUp
(); //SettingPath is an extremely Model <SettingPath>(("SettingPath"));
public class HomeController { public SettingPath settingPath; private ILog log = (, typeof(VideosController)); public HomeController(IOptions<SettingPath> option) { settingPath = ; } public void GetVideoPath() { string path= } }
Because I don't know how IOptions are transmitted here, I don't know what to do if I need to use only two or more models.
.net core reads configuration file public class
I have used the previous methods before, but I personally feel that they are not very easy to use. And if you want to read configuration files in a class library, it is so painful that you don’t want to care about your wife.
So I wrote a tool class by myself
using ; using ; using ; using ; using System; namespace Common { public class ConfigurationHelper { public IConfiguration config { get; set; } public ConfigurationHelper() { IHostingEnvironment env = <IHostingEnvironment>(); config = new ConfigurationBuilder() .SetBasePath() .AddJsonFile("", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{}.json", optional: true) .AddEnvironmentVariables() .Build(); } public T GetAppSettings<T>(string key) where T : class, new() { var appconfig = new ServiceCollection() .AddOptions() .Configure<T>((key)) .BuildServiceProvider() .GetService<IOptions<T>>() .Value; return appconfig; } } //I prefer to put this class alone, but it is more obvious public class MyServiceProvider { public static IServiceProvider ServiceProvider { get; set; } } }
To use this class, you need to add it in the Configure of StartUp
= ;
Then you can use this type to read configuration file information anywhere, and because the environment variables have been loaded by default when ConfigurationHelper is initialized, it also has multi-environment functions.
string path = new ConfigurationHelper().config["RedisPath"]; SettingPath pathss = new ConfigurationHelper().GetAppSettings<SettingPath>("SettingPath");
refer to
- /zh-cn/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.1
- /zh-cn/aspnet/core/fundamentals/environments?view=aspnetcore-2.1
- https:///article/
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.