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/UserSecrets

Configuration

https://docs.asp.net/en/latest/fundamentals/configuration.html

http://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 配置管理的更多相关文章

  1. ASP.NET Core 1.0 开发记录

    官方资料: https://github.com/dotnet/core https://docs.microsoft.com/en-us/aspnet/core https://docs.micro ...

  2. [转]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 ...

  3. .NET跨平台之旅:将示例站点从 ASP.NET 5 RC1 升级至 ASP.NET Core 1.0

    终于将“.NET跨平台之旅”的示例站点 about.cnblogs.com 从 ASP.NET 5 RC1 升级至 ASP.NET Core 1.0 ,经历了不少周折,在这篇博文中记录一下. 从 AS ...

  4. [译]Writing Custom Middleware in ASP.NET Core 1.0

    原文: https://www.exceptionnotfound.net/writing-custom-middleware-in-asp-net-core-1-0/ Middleware是ASP. ...

  5. 初识ASP.NET Core 1.0

    本文将对微软下一代ASP.NET框架做个概括性介绍,方便大家进一步熟悉该框架. 在介绍ASP.NET Core 1.0之前有必要澄清一些产品名称及版本号.ASP.NET Core1.0是微软下一代AS ...

  6. ASP.NET Core 1.0 静态文件、路由、自定义中间件、身份验证简介

    概述 ASP.NET Core 1.0是ASP.NET的一个重要的重新设计. 例如,在ASP.NET Core中,使用Middleware编写请求管道. ASP.NET Core中间件对HttpCon ...

  7. 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= ...

  8. 在 Mac OS 上创建并运行 ASP.NET Core 1.0 网站

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  9. ASP.NET Core 1.0 中的依赖项管理

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

随机推荐

  1. hibernate4.3.5,Final hibernate.cfg.xml的配置

    今天是接触hibernate的第二天,用来练习的是hibernate最新的版本hibernate-release-4.3.5.Final 要使用hibernate,则先要引入它的jar包,要引入的ja ...

  2. VIP之MixerII

    1.VIP Mixer IIMixerII的每一个输入通道都必须通过Frame Buffer来或者Frame Reader驱动,才能保证数据在正确的时间送入到MixerII中.Downscale不能把 ...

  3. hibernate映射组成关系

    目录结构 类 package com.hibernate.helloworld; public class School { private String name; private String a ...

  4. Group精确定位(canvas定位)

    ## <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> &l ...

  5. Java应用分类

    Java应用分类     一.应用程序.指在操作系统上直接运行的,不是浏览器,Java环境用本机的,需要在客户端安装,Java环境可以一起安装.         1.GUI图形界面应用程序       ...

  6. day23(事务管理)

    事务管理 事务管理两种方式: 向下传递,ThreadLocal 向下传递的方式(依赖) 缺点:不利于测试 Service层 获取连接conn(Connection) 转账(conn) 收账(conn) ...

  7. 【repost】 原生JS执行环境与作用域深入理解

    首先,我们要知道执行环境和作用域是两个完全不同的概念. 函数的每次调用都有与之紧密相关的作用域和执行环境.从根本上来说,作用域是基于函数的,而执行环境是基于对象的(例如:全局执行环境即window对象 ...

  8. noip第12课资料

  9. 第78讲:Type与Class实战详解

    今天来学习下type与class解析 让我们先来看看代码 import scala.reflect.runtime.universe._ class Sparktrait Hadoopobject F ...

  10. STL-容器库101--array【C11】

    1. 原型 C11提供 template < class T, size_t N > class array; T: 元素类型,以 array::value_type 作为别名使用:N: ...