ASP.NET 5: New configuration files and containers
2015-03-02 17:36
561 查看
原文: http://gunnarpeipman.com/2014/11/asp-net-5-new-configuration-files-and-containers/
ASP.NET vNext提供了一种新的config文件. 能支持多种格式的config文件可以是.json .ini .xml. 另外你还可以写configuration handler用来处理你自定义格式的config文件.
config.json
config.xml
config.ini
上面的3个config都是一种结构尽管他们是不同的格式.
现在configuration文件已经加载进来了,现在让我们使用强大的configuration对象来读取config参数.
![](http://images.cnitblog.com/blog/702742/201503/021701355044231.png)
如果你像读取非string类型的参数你可以使用泛型方法Get<T>():
我们可以使用多个configurationcontainers
我们可以在web和桌面程序中使用同样的配置文件.
看看下面的代码.
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参数
![](http://images.cnitblog.com/blog/702742/201503/021701355044231.png)
如果你像读取非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”);
相关文章推荐
- Preview Word files (docx) in HTML using ASP.NET, OpenXML and LINQ to XML
- Upload Files Using ASP.NET Impersonation and UNC Share
- Asp.Net Globalization and Localization using Local Resource files and Global Resource files
- Use ASP.NET and DotNetZip to Create and Extract ZIP Files
- Customize web.config sections and register your own configuration then get values in asp.net
- An Overview Of The New Services, Controls, And Features In ASP.NET 2.0
- Multiple Files Upload in ASP.NET and ASP.NET with jQuery
- WCF REST Configuration for ASP.NET AJAX and plain REST Services
- New ASP.NET Features and Fixes in Visual Studio 2015 RC
- Creating and writing ASP.NET 2.0 custom Configuration Sections
- asp.net core1.0 tutorials and new features
- WCF REST Configuration for ASP.NET AJAX and plain REST Services
- Preview Word files (docx) in HTML using ASP.NET, OpenXML and LINQ to XML
- ASP.NET MVC Uploading and Downloading Files
- ExportAttribute, ImportAttribute, CompositionContainer and MEF in ASP.NET MVC 3
- New <%: %> Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)
- (全英文)How to install and run a simple Asp.Net 5 Application in a Docker Container
- Updated ASP.NET AJAX Control Toolkit Release and New ASP.NET AJAX Videos/Articles
- ASP.NET MVC 3 (Adding a New Field to the Movie Model and Table) (8/9)
- ASP.NET MVC 3 (Adding a New Field to the Movie Model and Table) (8/9)