一、添加NuGet 包:
1. Microsoft.Extensions.Configuration.Binder:將對象與 Microsoft.Extensions.Configuration 的配置提供程序中的數(shù)據(jù)綁定的功能。
2.Microsoft.Extensions.Configuration.Json:Microsoft.Extensions.Configuration 的 JSON 配置提供程序實現(xiàn)。
3. Microsoft.Extensions.Configuration.EnvironmentVariables:Microsoft.Extensions.Configuration 的環(huán)境變量配置提供程序實現(xiàn)。
二、在項目中添加JSON配置文件?appsettings.json?
{
? "DB": {
? ? "DBType": "MySql",
? ? "ConnectionString": "server=localhost;userid=root;pwd=root;port=3306;database=data;sslmode=none;"
? }
三、代碼文章來源:http://www.zghlxwxcb.cn/news/detail-449534.html
?? ??? ??? ?IConfiguration config = new ConfigurationBuilder()
?? ??? ??? ??? ?.SetBasePath(Directory.GetCurrentDirectory())
?? ??? ??? ??? ?.AddJsonFile("appsettings.json")//將 JSON 配置源添加到 builder
?? ??? ??? ??? ?.AddEnvironmentVariables()//添加從環(huán)境變量讀取配置值的 IConfigurationProvider
?? ??? ??? ??? ?.Build();
?? ??? ?
?? ??? ??? ?string dbType=? config["DB:DbType"];
? ? ? ? ? ? string connStr =?config["DB:ConnectionString"];文章來源地址http://www.zghlxwxcb.cn/news/detail-449534.html
到了這里,關于NetCore讀取JSON配置文件的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!