读取appSetting文件
配置文件如下
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"WebSetting": {
"WebName": "ASP.NET Core",
"Title": "Hello Title",
"Behavior": {
"IsCheckIp": true,
"MaxConnection": 300
}
}
}
方法一、键读取
首先,在类构造函数注入 IConfiguration,例如:
public IConfiguration Configuration { get; } public ValuesController(IConfiguration configuration) { Configuration = configuration; }
然后就可以在该类任何方法读取:
[HttpGet] public ActionResult<IEnumerable<string>> Get() { var title = Configuration["WebSetting:Title"]; var isCheckIp = Configuration["WebSetting:Behavior:IsCheckIp"]; return new string[] { title, isCheckIp }; }
方法二、绑定读取
首先,还是类构造函数注入ICofiguration
public IConfiguration Configuration { get; } public ValuesController(IConfiguration configuration) { Configuration = configuration; }
然后,创建配置项结构对应的配置模型
public class Behavior { public bool IsCheckIp { get; set; } public int MaxConnection { get; set; } } public class WebSetting { public string WebName { get; set; } public string Title { get; set; } public Behavior Behavior { get; set; } }
最后如下方式读取配置
[HttpGet] public ActionResult<IEnumerable<string>> Get() { var webSetting = new WebSetting(); Configuration.GetSection("WebSetting").Bind(webSetting); var webName = webSetting.WebName; var isCheckIp = webSetting.Behavior.IsCheckIp; return new string[] { webName, isCheckIp.ToString() }; }
方法三、注册读取
首先,创建配置项结构对应的配置模型
public class Behavior { public bool IsCheckIp { get; set; } public int MaxConnection { get; set; } } public class WebSetting { public string WebName { get; set; } public string Title { get; set; } public Behavior Behavior { get; set; } }
然后,在 ConfigureServices 方法中注册
public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); //注册配置 services.Configure<WebSetting>(Configuration.GetSection("WebSetting")); }
最后读取配置:在读取配置的类构造函数注入IOptions,就可以在该类任何位置读取配置如下
1、构造函数注入读取
private IOptions<WebSetting> webSettingOption; //构造函数 public ValuesController(IOptions<WebSetting> _webSettingOption) { webSettingOption = _webSettingOption; } [HttpGet] public ActionResult<IEnumerable<string>> Get() { var webName = webSettingOption.Value.WebName; var isCheckIp = webSettingOption.Value.Behavior.IsCheckIp; return new string[] { webName, isCheckIp.ToString() }; }
自定义配置文件
首先新建配置文件,名称customSetting.json,并右击该文件选择属性进行设置,设置复制到输出目录:始终复制
{ "Name": "Rick", "Age": 99, "TicketSetting": { "BaseHost": "www.baidu.com", "Port": 9950 } }
然后定义配置文件结果类
public class CustomSetting { public string Name { get; set; } public int Age { get; set; } public TicketSetting TicketSetting { get; set; } } public class TicketSetting { public string BaseHost { get; set; } public int Port { get; set; } }
其次ConfigureServices方法进行配置注册
public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); var config = new ConfigurationBuilder() .AddJsonFile("customSetting.json") .Build(); services.Configure<CustomSetting>(config); }
最后读取
private IOptions<CustomSetting> _option; public ValuesController(IOptions<CustomSetting> option) { _option = option; } [HttpGet] public ActionResult<IEnumerable<string>> Get() { var host = _option.Value.TicketSetting.BaseHost; return new string[] { host }; }