在appsettings.json
{
"MyArray": [
"str1",
"str2",
"str3"
]
}
在Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IConfiguration>(Configuration);
}
在HomeController
public class HomeController : Controller
{
private readonly IConfiguration _config;
public HomeController(IConfiguration config)
{
this._config = config;
}
public IActionResult Index()
{
return Json(_config.GetSection("MyArray"));
}
}
上面是我的代码。结果是零。
如何获取数组?
在应用程序设置中添加一个级别。json:
{
"MySettings": {
"MyArray": [
"str1",
"str2",
"str3"
]
}
}
创建一个代表你的section的类:
public class MySettings
{
public List<string> MyArray {get; set;}
}
在你的应用启动类中,绑定你的模型并将其注入到DI服务中:
services.Configure<MySettings>(options => Configuration.GetSection("MySettings").Bind(options));
在你的控制器中,从DI服务中获取配置数据:
public class HomeController : Controller
{
private readonly List<string> _myArray;
public HomeController(IOptions<MySettings> mySettings)
{
_myArray = mySettings.Value.MyArray;
}
public IActionResult Index()
{
return Json(_myArray);
}
}
你也可以把你的整个配置模型存储在控制器的属性中,如果你需要所有的数据:
public class HomeController : Controller
{
private readonly MySettings _mySettings;
public HomeController(IOptions<MySettings> mySettings)
{
_mySettings = mySettings.Value;
}
public IActionResult Index()
{
return Json(_mySettings.MyArray);
}
}
ASP。NET Core的依赖注入服务就像一个魔法一样:)
.Net Core 7.x中处理对象的不同方法
在appsettings.json:
{
"People": [
{ "FirstName": "Glen", "LastName": "Johnson", "Age": 30 },
{ "FirstName": "Matt", "LastName": "Smith", "Age": 40 },
{ "FirstName": "Fred", "LastName": "Williams", "Age": 50 }
]
}
Person类:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
在代码中:
var appConfig = App.Current.AppConfiguration; // Or could be passed in through DI
var children = appConfig.GetSection("People")
.GetChildren()
.ToList();
var people = new List<Person>();
foreach (var child in children)
{
var rec = new Person
{
FirstName = appConfig[$"{child.Path}:FirstName"],
LastName = appConfig[$"{child.Path}:LastName"],
Age = int.Parse(appConfig[$"{child.Path}:Age"]),
};
people.Add(rec);
}
appsettings.json:
"MySetting": {
"MyValues": [
"C#",
"ASP.NET",
"SQL"
]
},
我的设置类:
namespace AspNetCore.API.Models
{
public class MySetting : IMySetting
{
public string[] MyValues { get; set; }
}
public interface IMySetting
{
string[] MyValues { get; set; }
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
...
services.Configure<MySetting>(Configuration.GetSection(nameof(MySetting)));
services.AddSingleton<IMySetting>(sp => sp.GetRequiredService<IOptions<MySetting>>().Value);
...
}
Controller.cs
public class DynamicController : ControllerBase
{
private readonly IMySetting _mySetting;
public DynamicController(IMySetting mySetting)
{
this._mySetting = mySetting;
}
}
访问值:
var myValues = this._mySetting.MyValues;
从appsettings.json中获取所有section的所有值
public static string[] Sections = { "LogDirectory", "Application", "Email" };
Dictionary<string, string> sectionDictionary = new Dictionary<string, string>();
List<string> sectionNames = new List<string>(Sections);
sectionNames.ForEach(section =>
{
List<KeyValuePair<string, string>> sectionValues = configuration.GetSection(section)
.AsEnumerable()
.Where(p => p.Value != null)
.ToList();
foreach (var subSection in sectionValues)
{
sectionDictionary.Add(subSection.Key, subSection.Value);
}
});
return sectionDictionary;