[译]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 ...
随机推荐
- Shell变量的定义与赋值操作注意事项
1 shell变量是弱类型 * 声明变量不用声明类型 * 可以存储不同类型的内容 * 使用时要明确变量的类型 * 区分大小写 2 变量声明及赋值格式 2.1 格式 变量名=变量值 # 注意等号两侧不 ...
- Bzoj1449 [JSOI2009]球队收益
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 741 Solved: 423 Description Input Output 一个整数表示联盟里所有球 ...
- karottc A Simple linux-virus Analysis、Linux Kernel <= 2.6.37 - Local Privilege Escalation、CVE-2010-4258、CVE-2010-3849、CVE-2010-3850
catalog . 程序功能概述 . 感染文件 . 前置知识 . 获取ROOT权限: Linux Kernel <= - Local Privilege Escalation 1. 程序功能概述 ...
- php Unable to find the wrapper "https"
php -m | grep -i --color openssl php 没有openssl模块 cd /data/source/php-5.3.29/ext/openssl #php的解压包下面 y ...
- Unity连Photon服务器入门详解
Photon是目前比较好用的游戏服务器.目前网上对于Photon的服务器讲解比较少,最近也对Photon做了初步的了解,做一个极其详细的入门. 首先就是得下载Photon咯 https://www.p ...
- CentOS 下安装
2016年12月5日15:25:58 ----------------------------------- 通常情况下在centos下安装软件就用yum. 关键是,使用yum你要知道安装包的名字是什 ...
- Possion 分布
泊松分布的概率函数为: \[P(X=k)=\frac{\lambda^k}{k!}e^{-\lambda},k=0,1,2,\cdots\] 如果 $X_i \sim P(\lambda_i)$,并 ...
- zabbix监控系列(3)之zabbix触发器格式配置
前言 今天公司线上环境新添一个监控项,监控 一些日志的是否出现错误,有错误就及时报警,那么问题来了 ,报警必须告诉我们哪个日志有错误,这样才能够快速定位到哪个日志有问题. 配置 zabbix-agen ...
- omnet++5.0安装使用
1.下载Windows安装包,5.0的omnetpp-5.0-src-windows.zip 2.解压到d盘 3.D:\omnetpp-5.0\doc找到这个目录,下面有个InstallGuide.p ...
- 自然语言14.1_python实现PorterStemmer算法
QQ:231469242 欢迎喜欢nltk朋友交流 #https://tartarus.org/martin/PorterStemmer/python.txt #!/usr/bin/env pytho ...