原文: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的更多相关文章

  1. 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 重点: 实现多级子目录的压缩, ...

  2. [译]Asp.net MVC 之 Contorllers(二)

    URL路由模块 取代URL重写 路由请求 URL路由模块的内部结构 应用程序路由 URL模式和路由 定义应用程序路由 处理路由 路由处理程序 处理物理文件请求 防止路由定义的URL 属性路由 书接上回 ...

  3. Struts – Multiple configuration files example

    Many developers like to put all Struts related stuff (action, form) into a single Struts configurati ...

  4. Environment Configuration Files

    Environment Configuration Files When a user logs in, an environment is created for that user automat ...

  5. Spring Configuration Check Unmapped Spring configuration files found

    Spring Configuration Check Unmapped Spring configuration files found 项目中有xml文件,但没有被用IntelliJ 导入现有工程时 ...

  6. [译]ASP.NET Core 2.0 系列文章目录

    基础篇 [译]ASP.NET Core 2.0 中间件 [译]ASP.NET Core 2.0 带初始参数的中间件 [译]ASP.NET Core 2.0 依赖注入 [译]ASP.NET Core 2 ...

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

  8. 出现unmapped spring configuration files found

    intell idea启动出现unmapped spring configuration files found提示. 把spring里面的内容都打勾.

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

随机推荐

  1. 分区还原工具(DiskGenius)

    不小心删除的分区可以实用DiskGenius进行还原. 下载地址:http://www.upantool.com/qidong/2012/DiskGenius_3.7.1.html 操作方法: 打开D ...

  2. poj1279 半平面交

    题意:没看懂= = sol:在纸上随便画两下就可以看出,答案即按逆时针方向建立line,求它们的半平面交的面积. 模板题.注意输出答案时输出ans+eps,否则可能会出现结果为-0.00的情况. #i ...

  3. 雷赛dmc2410控制卡,驱动器 光栅 加电机

    一般驱动器为9pin接口:(1:VCC 2:限位近端 3:限位远端 4:GND):电源是供给限位器的 ,5:A+ 6:A- 7:B+ 8:B- 注意驱动器的电流大小和细分设置,电流一般1A左右 电流过 ...

  4. soupui 使用

  5. PowerDesigner 学习笔记

    软件:PowerDesginer16.5 首先说一下对应的模型 New Model  --->  Model Type 作为一个英语渣,实在是压力颇大,然而汉化经常会导致无故的BUG,所以简单翻 ...

  6. UVa 11149 Power of Matrix(倍增法、矩阵快速幂)

    题目链接: 传送门 Power of Matrix Time Limit: 3000MS      Description 给一个n阶方阵,求A1+A2+A3+......Ak. 思路 A1+A2+. ...

  7. maven工程直接部署在tomcat上

    http://www.cnblogs.com/guodefu909/p/4874549.html (现在用的是第三点.)

  8. Objective-C之字典

    //字典:(关键字 值) //插入代码字太小 //        NSArray *array = [NSArray array];//空数组 //        NSDictionary *dict ...

  9. wpf comboBox取值问题

    这是获取值后台代码 private void button1_Click(object sender, RoutedEventArgs e)        {            combBox = ...

  10. mixin设计模式

    mixin可以轻松被一个子类或者一组子类继承,目的是函数复用.在js中,我们可以将继承MiXin看作为一种通过扩展收集功能的方式. e.mixin = function(t) { for (var i ...