C#如何从appsettings.json获取值
我正在尝试从appsettings.json中提取C#如何从appsettings.json获取值,c#,C#,我正在尝试从appsettings.json中提取 "BulkMailCodes": { "NcoaCorrectedCodes": { "A": "Full match", "91": "Matched despite missing secondary number", "92": "Matched despite extra secondary number" }, "
"BulkMailCodes": {
"NcoaCorrectedCodes": {
"A": "Full match",
"91": "Matched despite missing secondary number",
"92": "Matched despite extra secondary number"
},
"NcoaSuppressedCodes": {
"0": "No matching address",
"1": "New address is Outside US",
"2": "No forwarding address",
"3": "PO Box closed",
}
如何将“BulkMailCodes”:“NcoaCorrectedCodes”的键/值对获取到字典或KeyValuePair对象中
我有这门课:
class BulkMail
{
public static IConfigurationRoot configuration = GetConfig();
Dictionary<string, string> ncoaCorrected, ncoaSuppressed;
static void Main(string[] args)
{
BulkMail bulkMail = new BulkMail();
}
public BulkMail()
{
ncoaCorrected = configuration["BulkMailCodes: CassSuppressedCodes"];
Console.WriteLine("NcoaCorrectedCodes: ");
foreach (KeyValuePair<string, string> pair in ncoaCorrected)
{
Console.WriteLine(string.Format("{0}: {1}", pair.Key, pair.Value));
}
}
public static IConfigurationRoot GetConfig()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
return (builder.Build());
}
}
类邮件
{
公共静态IConfigurationRoot配置=GetConfig();
字典不正确,不正确;
静态void Main(字符串[]参数)
{
BulkMail BulkMail=新BulkMail();
}
公共邮件()
{
ncoaCorrected=配置[“BulkMailCodes:CassSuppressedCodes”];
Console.WriteLine(“NcoaCorrectedCodes:”);
foreach(ncoaCorrected中的KeyValuePair对)
{
WriteLine(string.Format(“{0}:{1}”,pair.Key,pair.Value));
}
}
公共静态IConfigurationRoot GetConfig()
{
var builder=new ConfigurationBuilder()
.SetBasePath(目录.GetCurrentDirectory())
.AddJsonFile(“appsettings.json”,可选:true,reloadOnChange:true);
返回(builder.Build());
}
}
谢谢 ncoaCorrected=configuration.GetSection(“BulkMailCodes:NcoaCorrectedCodes”).GetChildren()
ncoaCorrected = configuration.GetSection("BulkMailCodes:NcoaCorrectedCodes").GetChildren()
.ToDictionary(x => x.Key, x => x.Value);
Console.WriteLine("NcoaCorrectedCodes: ");
foreach (KeyValuePair<string, string> pair in ncoaCorrected)
{
Console.WriteLine(string.Format("{0}: {1}", pair.Key, pair.Value));
}
.ToDictionary(x=>x.Key,x=>x.Value);
Console.WriteLine(“NcoaCorrectedCodes:”);
foreach(ncoaCorrected中的KeyValuePair对)
{
WriteLine(string.Format(“{0}:{1}”,pair.Key,pair.Value));
}
只需将其绑定到Dictionary类型的对象。
您需要添加一个金块包:Microsoft.Extensions.Configuration.Binder
您可以这样做:
class BulkMail
{
public static IConfigurationRoot configuration = GetConfig();
private Dictionary<string, string> ncoaCorrected = new Dictionary<string, string>();
private Dictionary<string, string> ncoaSuppressed = new Dictionary<string, string>();
static void Main(string[] args)
{
BulkMail bulkMail = new BulkMail();
}
public BulkMail()
{
configuration.GetSection("BulkMailCodes:NcoaCorrectedCodes").Bind(ncoaCorrected);
configuration.GetSection("BulkMailCodes:NcoaSuppressedCodes").Bind(ncoaSuppressed);
Console.WriteLine("NcoaCorrectedCodes: ");
foreach(KeyValuePair<string, string> pair in ncoaCorrected)
{
Console.WriteLine(string.Format("{0}: {1}", pair.Key, pair.Value));
}
Console.WriteLine("NcoaSuppressedCodes: ");
foreach(KeyValuePair<string, string> pair in ncoaSuppressed)
{
Console.WriteLine(string.Format("{0}: {1}", pair.Key, pair.Value));
}
Console.ReadLine();
}
public static IConfigurationRoot GetConfig()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
return (builder.Build());
}
}
类邮件
{
公共静态IConfigurationRoot配置=GetConfig();
私有字典ncoaCorrected=新字典();
私有字典ncoaSuppressed=新字典();
静态void Main(字符串[]参数)
{
BulkMail BulkMail=新BulkMail();
}
公共邮件()
{
configuration.GetSection(“BulkMailCodes:NcoaCorrectedCodes”).Bind(ncoaCorrected);
configuration.GetSection(“BulkMailCodes:NcoaSuppressedCodes”).Bind(ncoaSuppressed);
Console.WriteLine(“NcoaCorrectedCodes:”);
foreach(ncoaCorrected中的KeyValuePair对)
{
WriteLine(string.Format(“{0}:{1}”,pair.Key,pair.Value));
}
Console.WriteLine(“NcoaSuppressedCodes:”);
foreach(ncoaSuppressed中的KeyValuePair对)
{
WriteLine(string.Format(“{0}:{1}”,pair.Key,pair.Value));
}
Console.ReadLine();
}
公共静态IConfigurationRoot GetConfig()
{
var builder=new ConfigurationBuilder()
.SetBasePath(目录.GetCurrentDirectory())
.AddJsonFile(“appsettings.json”,可选:true,reloadOnChange:true);
返回(builder.Build());
}
}
这是可行的,但问题的真正根源是appsettings.json默认为“不复制”的文件属性中的“复制到输出”目录。我把它改成了“永远复制”,它立刻就起作用了。如果这是一个必需的设置,为什么默认为“不复制”是无法理解的。