循序渐进学.Net Core Web Api开发系列【6】:配置文件appsettings.json
系列目录
本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi
一、本篇概述
本篇描述appsettings.json的使用,包括:
1、配置的基本读取
2、读取配置信息到自定义的对象
3、自定义配置文件
一、配置的基本读取
要读取的配置文件内容如下:
{ "ConnString": "MySQL Connect String", "Logging": { "IncludeScopes": false, "Debug": { "LogLevel": { "Default": "Warning" } }, "Console": { "LogLevel": { "Default": "Warning" } } }, "SystemConfig": { "UploadFile": "d:\\files", "AnnexUrl": "http://192.168.0.177:81/", "Admin": { "Name": "admin", "Age": "31", "Allow": "True" }, "DefaultPassword": "123456" } }
读取配置文件的方法如下:
public class Startup { public Startup(IConfiguration configuration,IHostingEnvironment env) { Configuration = configuration; _env = env; } public IConfiguration Configuration { get; } public IHostingEnvironment _env { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc();
var ConnString = Configuration["ConnString"]; var UploadFile = Configuration.GetSection("SystemConfig")["UploadFile"]; var AdminName = Configuration.GetSection("SystemConfig").GetSection("Admin")["Name"]; } }
还有一种读法:
ConnString = Configuration["ConnString"]; UploadFile = Configuration["SystemConfig:UploadFile"]; AdminName = Configuration["SystemConfig:Admin:Name"];
这里Startup类在构造时已经帮我们注入了Configuration,如果要在自己的Controller内使用,需要自己注入。
public class ValuesController : Controller { private IConfiguration _configuration; public ValuesController(IConfiguration configuration) { _configuration = configuration; } [HttpGet] public IEnumerable<string> Get() { var ConnString = _configuration["ConnString"]; return new string[] { "value1", "value2" }; } }
二、读取配置信息到自定义的对象
新建一个类,用来存储配置信息,类的结构应和配置文件一致。
public class SystemConfig { public String UploadFile { get; set; } public String AnnexUrl { get; set; } public User Admin { get; set; } public String DefaultPassword { get; set; } } public class User { public String Name { get; set; } public int Age { get; set; } public bool Allow { get; set; } }
在startup类的ConfigureServices方法内,提供如下代码:
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddOptions(); services.Configure<SystemConfig>(Configuration.GetSection("SystemConfig")); } } }
然后在Controller中进行注入并使用。
public class ValuesController : Controller { private IOptions<SystemConfig> _setting; public ValuesController( IOptions<SystemConfig> setting) { _setting = setting; } [HttpGet("setting")] public IEnumerable<string> GetSetting() { var UploadFile = _setting.Value.UploadFile; var AdminName = _setting.Value.Admin.Name; var AdminAge = _setting.Value.Admin.Age; var AdminAllow = _setting.Value.Admin.Allow; return new string[] { "value1", "value2" }; } }
可以看到,这样在业务方法内通过 _setting 来都取配置信息就非常方便了。
三、自定义配置文件
以上操作的是系统默认的配置文件appsettings.json。如果我们需要增加自己的配置文件该如何处理?
新建一个配置文件:mysetting.json
{ "ConnString": "MySQL Connect String", "SystemConfig": { "UploadFile": "d:\\myfiles", "AnnexUrl": "http://192.168.0.177:81/my", "Admin": { "Name": "myadmin", "Age": "131", "Allow": "False" }, "DefaultPassword": "654321" } }
在Startup类的ConfigureServices方法输入以下代码:
public class Startup { public Startup(IConfiguration configuration,IHostingEnvironment env) { Configuration = configuration; _env = env; } public IConfiguration Configuration { get; } public IHostingEnvironment _env { get; } public void ConfigureServices(IServiceCollection services) { services.AddMvc(); var rootpath = _env.ContentRootPath; var builder = new ConfigurationBuilder() .SetBasePath(_env.ContentRootPath) .AddJsonFile("mysetting.json",optional: true, reloadOnChange: true) .AddEnvironmentVariables(); var MyConfiguration = builder.Build(); services.AddOptions(); services.Configure<SystemConfig>(MyConfiguration.GetSection("SystemConfig")); } }
用自己创建的Configuration进行服务的注册,创建过程中需要用到IHostingEnvironment,这个对象在Startup类构建时进行注入。
剩下的用法和默认配置用法就一样了。
签名区:
如果您觉得这篇博客对您有帮助或启发,请点击右侧【推荐】支持,谢谢!