[译]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 ...
随机推荐
- Android成长日记-数据存储之SharedPreferences
数据篇-SharedPreferences Android的四种存储方式 1. SharedPreferences 2. SQLite 3. Content Provider 4. File ---- ...
- dedecms /plus/feedback.php SQL Injection Vul
catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 . Dedecms v5.7的plus\feedback.php SQL ...
- Busting Frame Busting: a Study of Clickjacking Vulnerabilities on Popular Sites
Busting Frame Busting Reference From: http://seclab.stanford.edu/websec/framebusting/framebust.pdf T ...
- Java内存回收机制
在Java中,它的内存管理包括两方面:内存分配(创建Java对象的时候)和内存回收,这两方面工作都是由JVM自动完成的,降低了Java程序员的学习难度,避免了像C/C++直接操作内存的危险.但是,也正 ...
- ApkTool反编译apk,去除广告或者汉化后重新打包apk,并签名
http://my.oschina.net/mopidick/blog/135581 以ubuntu的环境为例子,window的大同小异 首先下载apktool工具:https://code.goog ...
- mybatis报错Mapped Statements collection does not contain value for com.inter.IOper
首页 > 精品文库 > mybatis报错Mapped Statements collection does not contain value for com.inter.IOper m ...
- html代码规范
HTML代码规范 我们知道,前端工程师入门容易,通过学习基本的HTML和CSS就能在浏览器上看到实际的效果,可是要写好的HTML,就不是那么容易了.这里将和大家分享HTML规范,希望大家读完之后都 ...
- wpf 窗体内容旋转效果 网摘
<Window x:Class="simplewpf.chuangtixuanzzhuan" xmlns="http://schemas.micros ...
- javascript基础之打印乘法表
废话不多说,直接上代码!! 代码如下: for(var i =1; i<=9;i++){ for(var j =1; j<=i;j++){ document.write(i+"* ...
- C#----操作应用程序配置文件App.config
对配置文件的一些疑问: 在应用程序的目录下,有两处值得注意的地方,一个是应用程序根目录下的App.config文件,和bin\debug\name.exe.config 或者 bin\Release\ ...