原文: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. HC-05初探

    catalogue . 蓝牙嗅探抓包 . HC05蓝牙模块AT模式设置 . USB转串口芯片CH340 . 蓝牙小车 1. 蓝牙嗅探抓包 针对蓝牙通信包的嗅探抓包不能直接使用wincap+wiresh ...

  2. C#关闭窗口代码

    if (MessageBox.Show("请您确认是否退出(Y/N)", "系统提示", MessageBoxButtons.YesNo, MessageBox ...

  3. 关于当一个C#工程移植到另一台机子上(win7)上时,程序报错。dll没有被指定在Windows上运行,或者它包含错误。请尝试使用原始安装媒体重新安装程序。。。。。。

    , 解决方法:通过从网上重新下载dll文件 拷贝到报错的目录下,替换掉原有的dll,可以正确运行.

  4. 数据结构算法C语言实现(二十七)--- 7.2图的遍历

    一.简述 栈与队列,DFS与BFS.仅以连接表为例实现. 二.头文件 BFS要用到的头文件 //3_4_part1.h /** author:zhaoyu email:zhaoyu1995.com@g ...

  5. 基本概率分布Basic Concept of Probability Distributions 5: Hypergemometric Distribution

    PDF version PMF Suppose that a sample of size $n$ is to be chosen randomly (without replacement) fro ...

  6. Tyvj P1175 机器人

    P1175 机器人 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 设a,b是给定的正整数.现有一机器人沿着一个有n级的楼梯上下.机器人每上升一次,恰好上升a ...

  7. Django admin coercing to Unicode: need string or buffer, tuple found

    见 http://stackoverflow.com/questions/29762306/django-admin-coercing-to-unicode-need-string-or-buffer ...

  8. sql 时间查询 /sql中判断更新或者插入/查询一年所有双休日

    ') update [DBPersonnel].[dbo].[TB証明書] ' else INSERT INTO [DBPersonnel].[dbo].[TB証明書] ([社員番号],[身分証明書] ...

  9. JQuery------prevAll(),nextAll(),attr()方法的使用

    $(this).nextAll(".Rec").find("input").attr("checked", false); $(this). ...

  10. 给IOS系统的微信页面赋Title

    给页面赋一个title是最平常不过的事情了,不过在IOS下动态给页面赋title可不是平常的事情. 看代码: function setIOStitle(title) { $body = $('body ...