- 配置提供商
- 文件格式(INI, JSON, XML)
- 命令行参数
- 环境变量
- 内存中的.NET对象
- 未加密的Secret管理存储
- 加密的用户存储, 例如Azure秘钥库
- 自定义的提供商
- JSON配置文件
- 默认
appsettings.json
注意:后加载的值会覆盖。
- 取得配置
- IConfiguration[“Key:ChildKey”]
- 针对”ConnectionStrings:xxx”, 可以使用IConfiguration. GetConnectionString(“xxx”)
public static IConfiguration Configuration;
public StartupDevelopment(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
//注册Dbcontxt
services.AddDbContext<ApplicationContext>(options =>
{
//var connectionString = "Data Source = 10.30.0.14; Database = Blog.db; User = sa; Password = Hello,MMOC";
//var connectionString = "Data Source = .;Database = Blog.db;Integrated Security=True";
//var connectionString = Configuration["ConnectionStrings:DefaultConnection"];
var connectionString = Configuration.GetConnectionString("DefaultConnection");
options.UseSqlServer(connectionString);
});
...
}
- 其他文件配置
ConfigurationBuilder().AddJsonFile("appsettings.json").Build() 返回IConfigurationRoot,继承于IConfiguration. 使用接口IConfiguration即可。