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= ...
随机推荐
- DOM10-1节点层次
DOM(问的那个对象模型)是针对HTML和XML文档的API.DOM描绘了一个层次化的节点树,允许开发人员添加.移除和修改页面的一部分. 每个节点都拥有各自的特点.数据和方法,另外和其他节点也存在某种 ...
- poj 1328 Radar Installation(贪心+快排)
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- 安装mysql后必做的两件事
1..删除掉不需要的用户 查看用户表mysql> SELECT User,Host FROM mysql.user; +------+-------------------------+ | U ...
- #微码分享#AES算法的C++包装类
AES为Advanced Encryption Standard的缩写,中文名:高级加密标准,在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准,用来替代DES.基于std:: ...
- 一份不太简短的LaTeX模板
编译环境: Ubuntu16.04 texllive2016 sublime text3 + latextools 该模板使用与自己写文档,记笔记,记录代码,写作业等等. %!TEX program ...
- (BestCoder Round #64 (div.2))Array
BestCoder Round #64 (div.2) Array 问题描述 Vicky是个热爱数学的魔法师,拥有复制创造的能力. 一开始他拥有一个数列{1}.每过一天,他将他当天的数列复制一遍,放在 ...
- web-day9
第9章WEB09-Servlet篇 今日任务 完成系统的登录的功能 完成登录系统后页面定时跳转 记录系统登录成功后,系统被访问多少次 教学导航 教学目标 了解HTTP协议 掌握Servlet的编写 了 ...
- Mac 下netstat和linux下不一样
Linux's netstat command options and OS X(/BSD)'s have almost nothing to do with each other. Of the o ...
- 关于jdbc连接MySQL数据问题
1.解压MySQL后配置环境变量 MYSQL_HOME:D:\win7\Program Files(x86)\mysql-5.6.21-win32(mysql根目录) 添加path:%MYSQL_HO ...
- CentOS中自动加载802.1q模块
要想在CentOS中自动加载内核模块,需要在/etc/sysconfig/modules/目录中增加一个脚本,在此脚本中加载所需的模块. 下面是我所用的一个名为8021q.modules的脚本,用来在 ...