[译]ASP.NET 5: New configuration files and containers
原文:http://gunnarpeipman.com/2014/11/asp-net-5-new-configuration-files-and-containers/
ASP.NET vNext提供了一种新的config文件. 能支持多种格式的config文件可以是.json .ini .xml. 另外你还可以写configuration handler用来处理你自定义格式的config文件.
Configuration文件
假设我们有3个config文件
config.json
{
"ConnectionStrings": {
"JsonConnectionString": "json connection string"
}
}
config.xml
<config>
<ConnectionStrings>
<XmlConnectionString>xml connection string</XmlConnectionString>
</ConnectionStrings>
</config>
config.ini
[ConnectionStrings]
IniConnectionString = ini connection string
上面的3个config都是一种结构尽管他们是不同的格式.
依赖
在我们加载config文件前我们需要添加一些依赖 打开project.json添加倒数第二行和第三行(Microsoft.Framework.Configuration.XXX):
{
// ...
"dependencies": {
"Kestrel": "1.0.0-alpha4",
"EntityFramework.SqlServer": "7.0.0-alpha4",
// ...
"Microsoft.AspNet.StaticFiles": "1.0.0-alpha4",
"Microsoft.Framework.ConfigurationModel.Json": "1.0.0-alpha4",
"Microsoft.Framework.ConfigurationModel.Xml": "1.0.0-alpha4",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-alpha4"
}
// ...
}
加载configuration文件
public class Startup
{
public void Configure(IApplicationBuilder app)
{
// Setup configuration sources
var configuration = new Configuration();
configuration.AddJsonFile(“config.json”);
configuration.AddIniFile(“config.ini”);
configuration.AddXmlFile(“config.xml”);
configuration.AddEnvironmentVariables(); // …
}
}
现在configuration文件已经加载进来了,现在让我们使用强大的configuration对象来读取config参数.
读取configuration参数

如果你像读取非string类型的参数你可以使用泛型方法Get<T>():
var size = configuration.Get<int>(“Box:Size”);
为什么我们需要多个configuration文件
为什么我们需要configuration系统是这个样子的? 通过这样的configuration系统以下的场景得到支持:
- 我们可以使用多个configurationcontainers
- 我们可以在web和桌面程序中使用同样的配置文件.
看看下面的代码.
// Common configuration
var configuration = new Configuration();
configuration.AddJsonFile(“config.json”);
configuration.AddIniFile(“config.ini”);
configuration.AddXmlFile(“config.xml”);
configuration.AddEnvironmentVariables(); // Mailer configuration
var emailConfiguration = new Configuration();
emailConfiguration.AddIniFile(“advanced-mailer.ini”); // SMS configuration
var smsConfiguration = new Configuration();
smsConfiguration.AddXmlFile(“sms.config”);
[译]ASP.NET 5: New configuration files and containers的更多相关文章
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
C#实现多级子目录Zip压缩解压实例 参考 https://blog.csdn.net/lki_suidongdong/article/details/20942977 重点: 实现多级子目录的压缩, ...
- [译]Asp.net MVC 之 Contorllers(二)
URL路由模块 取代URL重写 路由请求 URL路由模块的内部结构 应用程序路由 URL模式和路由 定义应用程序路由 处理路由 路由处理程序 处理物理文件请求 防止路由定义的URL 属性路由 书接上回 ...
- Struts – Multiple configuration files example
Many developers like to put all Struts related stuff (action, form) into a single Struts configurati ...
- Environment Configuration Files
Environment Configuration Files When a user logs in, an environment is created for that user automat ...
- Spring Configuration Check Unmapped Spring configuration files found
Spring Configuration Check Unmapped Spring configuration files found 项目中有xml文件,但没有被用IntelliJ 导入现有工程时 ...
- [译]ASP.NET Core 2.0 系列文章目录
基础篇 [译]ASP.NET Core 2.0 中间件 [译]ASP.NET Core 2.0 带初始参数的中间件 [译]ASP.NET Core 2.0 依赖注入 [译]ASP.NET Core 2 ...
- Inspection info: Checks Spring Boot application .properties configuration files. Highlights unresolved and deprecated configuration keys and in
Cannot resolve class or package ‘jdbc’ less… (Ctrl+F1) Inspection info: Checks Spring Boot applicati ...
- 出现unmapped spring configuration files found
intell idea启动出现unmapped spring configuration files found提示. 把spring里面的内容都打勾.
- magento: Your web server is configured incorrectly. As a result, configuration files with sensitive information are accessible from the outside 解决方案
在linux(以UBUNTU, CENTOS为例)下安装完成magento时,在进入后台时, 有些童鞋可能会发现有如下的提示: Your web server is configured incorr ...
随机推荐
- text-indent无效解决方案
text-indent是用来字符缩进的. 1.text-indent所在的元素是行内元素而非块级元素.比如用在span,a等行内元素上.解决方案:在行内元素加上display:block; 或者把目标 ...
- CruiseControl.NET/CCNET安装包下载
链接:http://pan.baidu.com/s/1bHgeCI 密码:d7q1
- MVC5-8 ViewData、ViewBag、TempData分析
MVC中Contoller与视图的数据传输 后台的值显示到界面上,我们有几种方式呢.MVC给我们提供了ViewData.ViewBag.TempData.Model这几种方式,当然我们也可以用ajax ...
- 使用Eval()绑定数据时使用三元运算符
ASP.NET邦定数据“<%#Eval("Sex")%>”运用三元运算符: <%#(Eval("Sex", "{0}") ...
- CF 363B One Bomb(枚举)
题目链接: 传送门 One Bomb time limit per test:1 second memory limit per test:256 megabytes Description ...
- 使用substring方法进行字符串拆分
对一个字符串进行操作,我们通常会用到这2个类:String类.StringBuffer类 而这2个类中的方法大多都是相同的,今天主要介绍他俩共同的一个特别有用的方法:substring substri ...
- Python3 学习笔记------迭代器
python 迭代器 要理解python迭代器(iterator),先要理解两个概念:Iterable(可迭代对象).Iterator(迭代器) 先来help()一下Iterator: >> ...
- tmux(ubuntu14.04)编译过程遇到的错误
1. configure: error: "libevent not found" 解决方法: $ git clone https://github.com/libevent/li ...
- easyUI中tree的简单使用
一.在JS中的代码 $('#tt').tree({ url: baseCtx + 'lib/easyui-1.4/demo/tree/tree_data1.json',//tree数据的来源,json ...
- Photoshop 融合属性 Unity Shader
http://forum.unity3d.com/threads/free-photoshop-blends.121661/