In ASP .NET 5, you can map appsettings to a class. See this answer for an up-to-date example.
在ASP .NET 5中,您可以将appsettings映射到类。请参阅此答案以获取最新示例。
Code I've seen that does this typically looks like:
代码我已经看到这通常是这样的:
services.Configure<AppSettings>(Configuration.GetConfigurationSection("AppSettings"));
So it's expected that you have a node with that name in your appsettings.json
(example taken from this article):
因此,您期望在appsettings.json中有一个具有该名称的节点(示例来自本文):
{
"AppSettings" : {
"SiteTitle" : "My Web Site"
},
"Data": {
"DefaultConnection": {
"ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;"
}
}
}
Is it possible to get the whole file (from the root) rather than having to declare a node such as AppSettings
?
是否可以获取整个文件(从根目录)而不必声明AppSettings等节点?
1 个解决方案
#1
1
Yes, you can. Use the ConfigurationBinder
是的你可以。使用ConfigurationBinder
var configurationBuilder = new ConfigurationBuilder();
// Add the providers that you want here
var config = configurationBuilder.Build();
var options = new StrongTypeObject();
config.Bind(options);
More examples: https://github.com/aspnet/Configuration/blob/dev/test/Microsoft.Extensions.Configuration.Binder.Test/ConfigurationBinderTests.cs
更多示例:https://github.com/aspnet/Configuration/blob/dev/test/Microsoft.Extensions.Configuration.Binder.Test/ConfigurationBinderTests.cs
#1
1
Yes, you can. Use the ConfigurationBinder
是的你可以。使用ConfigurationBinder
var configurationBuilder = new ConfigurationBuilder();
// Add the providers that you want here
var config = configurationBuilder.Build();
var options = new StrongTypeObject();
config.Bind(options);
More examples: https://github.com/aspnet/Configuration/blob/dev/test/Microsoft.Extensions.Configuration.Binder.Test/ConfigurationBinderTests.cs
更多示例:https://github.com/aspnet/Configuration/blob/dev/test/Microsoft.Extensions.Configuration.Binder.Test/ConfigurationBinderTests.cs