ASP.NET Core 1.0 Configuration 配置管理
documentation: https://docs.asp.net/en/latest/fundamentals/configuration.html
github: https://github.com/aspnet/Configuration/
项目结构

- 配置的接口定义与基础实现
- Microsoft.Extensions.Configuration 配置文件的基础实现
- Microsoft.Extensions.Configuration.Abstractions 配置文件的基础实现的接口定义
- Microsoft.Extensions.Configuration.Binder 特殊配置文件实现
- 配置的扩展
- Microsoft.Extensions.Configuration.CommandLine 命令行扩展
- Microsoft.Extensions.Configuration.EnvironmentVariables 环境变量扩展
- Microsoft.Extensions.Configuration.FileExtensions 文本类型扩展
- Microsoft.Extensions.Configuration.FileProviderExtensions 用来检测配置文本是否变动
- Microsoft.Extensions.Configuration.Ini Ini文件类型扩展
- Microsoft.Extensions.Configuration.Json Json文件类型扩展
- Microsoft.Extensions.Configuration.Xml Xml文件类型扩展
ASP.NET Core 1.0 中抛弃了原先的web.config文件机制,引用了现有的appsettings.json文件机制,配置的文件的类型可以是JSON,XML,INI等,如在Startup类中:
/// <summary>
/// 配置信息
/// </summary>
public IConfigurationRoot Configuration { get; set; } /// <summary>
/// 程序入口点
/// </summary>
/// <param name="env"></param>
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
新的配置机制基于Microsoft.Extensions.Configuration命名空间,IConfiguration接口中定义配置信息的实例接口
/// <summary>
/// Represents a set of key/value application configuration properties.
/// </summary>
public interface IConfiguration
{
/// <summary>
/// Gets or sets a configuration value.
/// </summary>
/// <param name="key">The configuration key.</param>
/// <returns>The configuration value.</returns>
string this[string key] { get; set; } /// <summary>
/// Gets a configuration sub-section with the specified key.
/// </summary>
/// <param name="key">The key of the configuration section.</param>
/// <returns>The <see cref="IConfigurationSection"/>.</returns>
/// <remarks>
/// This method will never return <c>null</c>. If no matching sub-section is found with the specified key,
/// an empty <see cref="IConfigurationSection"/> will be returned.
/// </remarks>
IConfigurationSection GetSection(string key); /// <summary>
/// Gets the immediate descendant configuration sub-sections.
/// </summary>
/// <returns>The configuration sub-sections.</returns>
IEnumerable<IConfigurationSection> GetChildren(); IChangeToken GetReloadToken();
}
IConfigurationRoot接口继承IConfiguration接口,定义了Reload方法; IConfigurationProvider 是定义所有实现的基础接口约定;IConfigurationBuilder接口是基础实现的构造器,ConfigurationBuilder类定义了基础具体实现。
扩展方法
支持多文件类型配置,是在IConfigurationBuilder构造器接口上进行扩展方法
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddIniFile(_iniConfigFilePath);
configurationBuilder.AddJsonFile(_jsonConfigFilePath);
configurationBuilder.AddXmlFile(_xmlConfigFilePath);
configurationBuilder.AddInMemoryCollection(_memConfigContent);
var config = configurationBuilder.Build();
如AddJsonFile扩展方法
/// <summary>
/// Adds the JSON configuration provider at <paramref name="path"/> to <paramref name="configurationBuilder"/>.
/// </summary>
/// <param name="configurationBuilder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="path">Absolute path or path relative to <see cref="IConfigurationBuilder.BasePath"/> of
/// <paramref name="configurationBuilder"/>.</param>
/// <param name="optional">Determines if loading the configuration provider is optional.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
/// <exception cref="ArgumentException">If <paramref name="path"/> is null or empty.</exception>
/// <exception cref="FileNotFoundException">If <paramref name="optional"/> is <c>false</c> and the file cannot
/// be resolved.</exception>
public static IConfigurationBuilder AddJsonFile(
this IConfigurationBuilder configurationBuilder,
string path,
bool optional)
{
if (configurationBuilder == null)
{
throw new ArgumentNullException(nameof(configurationBuilder));
} if (string.IsNullOrEmpty(path))
{
throw new ArgumentException(Resources.Error_InvalidFilePath, nameof(path));
} var fullPath = Path.Combine(configurationBuilder.GetBasePath(), path); if (!optional && !File.Exists(fullPath))
{
throw new FileNotFoundException(Resources.FormatError_FileNotFound(fullPath), fullPath);
} configurationBuilder.Add(new JsonConfigurationProvider(fullPath, optional: optional)); return configurationBuilder;
}
Application Secrets
https://github.com/aspnet/UserSecretsConfiguration
https://docs.asp.net/en/latest/fundamentals/configuration.htmlhttp://developer.telerik.com/featured/new-configuration-model-asp-net-core/
http://jameschambers.com/2016/01/Strongly-Typed-Configuration-in-ASP-NET-Core-MVC/
ASP.NET Core 1.0 Configuration 配置管理的更多相关文章
- ASP.NET Core 1.0 开发记录
官方资料: https://github.com/dotnet/core https://docs.microsoft.com/en-us/aspnet/core https://docs.micro ...
- [转]Writing Custom Middleware in ASP.NET Core 1.0
本文转自:https://www.exceptionnotfound.net/writing-custom-middleware-in-asp-net-core-1-0/ One of the new ...
- .NET跨平台之旅:将示例站点从 ASP.NET 5 RC1 升级至 ASP.NET Core 1.0
终于将“.NET跨平台之旅”的示例站点 about.cnblogs.com 从 ASP.NET 5 RC1 升级至 ASP.NET Core 1.0 ,经历了不少周折,在这篇博文中记录一下. 从 AS ...
- [译]Writing Custom Middleware in ASP.NET Core 1.0
原文: https://www.exceptionnotfound.net/writing-custom-middleware-in-asp-net-core-1-0/ Middleware是ASP. ...
- 初识ASP.NET Core 1.0
本文将对微软下一代ASP.NET框架做个概括性介绍,方便大家进一步熟悉该框架. 在介绍ASP.NET Core 1.0之前有必要澄清一些产品名称及版本号.ASP.NET Core1.0是微软下一代AS ...
- ASP.NET Core 1.0 静态文件、路由、自定义中间件、身份验证简介
概述 ASP.NET Core 1.0是ASP.NET的一个重要的重新设计. 例如,在ASP.NET Core中,使用Middleware编写请求管道. ASP.NET Core中间件对HttpCon ...
- ASP.NET Core 1.0 部署 HTTPS (.NET Framework 4.5.1)
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
- 在 Mac OS 上创建并运行 ASP.NET Core 1.0 网站
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
- ASP.NET Core 1.0 中的依赖项管理
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
随机推荐
- tomcat部署项目访问不加项目名方法
直接主题:tomcat部署项目访问不加项目名方法是打开tomcat的conf目录下server.xml文件 加入 <Context path="" docBase=" ...
- C++类、继承、多态、虚函数
一个比较好的虚函数例子 /****************************/ /* 作者:骆天 */ /* 时间:2018/1/26 */ /* 代码:多态的理解 */ /********** ...
- 笔记:CSS hack的学习与了解…
更新时间:2015.05.12 兼容范围: IE:6.0+,FireFox:2.0+,Opera 10.0+,Sarari 3.0+,Chrome 参考资料: 各游览器常用兼容标记一览表: 标记 I ...
- 解决UITableView上的cell的重用
1.通过为每个cell指定不同的重用标识符(reuseIdentifier)来解决 // static NSString *rankCellIndefier = @"rankC ...
- c 语言申明头文件和实现分开简单例子
很多时候,看到很多c函数的声明和实现是分开的.声明放在头文件,实现却放在另一个文件,最后函数被其他文件调用. 下面以简单例子说明. 一.声明部分 /* test.h */ #include <s ...
- Pychar-20170301快捷键
Pychar IDE 2017.03.03 版本的特性 ------------------------------------------------Ctrl+D:(Dumplicated) 复制选 ...
- openresty + lua 2、openresty 连接 redis,实现 crud
redis 的话,openresty 已经集成,ng 的话,自己引入即可. github 地址:https://github.com/openresty/lua-resty-redis github ...
- js-随机图片
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...
- 使用 ipmitool 实现远程管理Dell 系列服务器
IBM 文档: http://www.ibm.com/developerworks/cn/linux/l-ipmi/index.html ipmi命令收集: http://hi.baidu ...
- FormatSQL
核心提示:在一些论坛,常看到有人拼接SQL的时候,喜欢直接硬拼,结果就是出现一大堆加号和单引号,不仅写起来麻烦(你得小心该连续写多少个单引号),SQL的可读性也相当差....稍微好一点的方法是,使用Q ...