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= ...
随机推荐
- zend studio 修改字体大小
第一步:进入设置窗口 windows -> preferences第二步:进入修改字体的选项卡. General -> Appearance -> Colors and ...
- 堆操作,malloc
PS:堆空间缺省值都是cd,栈空间缺省值都是cc 内存有四区:栈.全局(静态).常量.除此以外的空间暂时不能随意使用,但是通过malloc函数申请就可以使用了. 利用malloc申请一个int变量,注 ...
- 20169207《Linux内核原理及分析》第十三周作业
第一周作业::对Linux的基本知识进行了了解,并对基本操作进行熟悉和应用. 第二周作业::了解了冯诺依曼体系结构.各种寄存器的功能和汇编指令的作用和功能. 第三周作业::这周主要了解了Linux系统 ...
- Properties类、序列化流与反序列化流、打印流、commons-IO
Properties类 特点: 1.Hashtable的子类,map集合中的方法都可以用: 2.该集合没有泛型,键值都是字符串: 3.是一个可以持久化的属性集,键值可以存到集合中,也可存到持久化的设备 ...
- eclipse生成可执行jar包(引入第三方.jar文件)
1. eclipse建立普通的java project项目(项目名aa) 2. 项目正常组织通过buildpath加载各种jar包入项目aa比如例子项目里,加入了spring 各种jar包加入各种配置 ...
- node API
看了这么久nodejs,它包含哪些基础的API,自己心中都没有数,该完整地看看基础的API了. 见过的: assert:编写程序的单元测试用例 buffer:直接处理2进制数据 child_proce ...
- shell中十种实现自加的方法
shell中十种实现自加的方法 let "n = $n + 1" : $((n = $n + )) ((n = n+)) n=$(($n + )) : $[ n = $n + ] ...
- [eetcode 10]Regular Expression Matching
1 题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- KNIME + Python = 数据分析+报表全流程
Python 数据分析环境 数据分析领域有很多可选方案,例如SPSS傻瓜式分析工具,SAS专业性商业分析工具,R和python这类需要代码编程类的工具.个人选择是python这类,包括pandas,n ...
- glob
主要是用来在匹配文件,相当shell中用通配符匹配. 用法: glob.glob(pathname) # 返回匹配的文件作为一个列表返回 glob.iglob(pathname) # 匹配到的文件名, ...