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. tinyweb集成springmvc 的一种可行方式

    最近tiny项目中集成了springmvc,而且使用的tiny的版本比较低,所以整合起来官网给的前两种方式都行不通. 而且有个tiny整合springmvc的maven依赖都下载不了.所以只有使用第三 ...

  2. C#时间操作总结

    命名空间:System.Diagnostics Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间.在典型的 Stopwatch 方案中,先调用 Start 方 ...

  3. timeSetEvent

    Header: Declared in Mmsystem.h; include Windows.h.Library: Use Winmm.lib. MSDN里定义的函数原型如下: MMRESULT t ...

  4. 读《.net设计规范》

    一.影响软件品质的影响有哪些?比如性能.可靠性.安全性.依赖性管理等. 二.客户先行的编程——如果让你把自己的程序库提供的功能描述出来,并让一个开发人员在没有看过该程序库的前提下, 根据他认为该程序库 ...

  5. 第一章javascript词法结构笔记摘要

    语法介绍 javascript最流行的脚本语言,用于web和HTML,服务器.pc.移动端 轻量级语言,可以插入HTML页面,由浏览器按编写顺序执行 一.字符集 用Unicode字符集编写,是ASCI ...

  6. War Chess (hdu 3345)

    http://acm.hdu.edu.cn/showproblem.php?pid=3345 Problem Description War chess is hh's favorite game:I ...

  7. js-设置时间,获取几天后的时间

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  8. 从0开始学Python---01

    1.开始 Vim  test.py #!/usr/bin/python print "hello,world!"; chmod +x test.py ./test.py 2.基本知 ...

  9. PICT用户手册 [转]

    PICT 3.3 User's Guide Jacek Czerwonka, Test Lead, Microsoft Corporation Overview Using PICT to Combi ...

  10. C#读取excel文件提示未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序

    错误描述: 在VS2010执行读取excel文件时,报错"未在本地计算机上注册"Microsoft.ACE.OLEDB.12.0"提供程序" 业务代码: //下 ...