NET Core开发-读取配置文件Configuration
ASP.NET Core开发-读取配置文件Configuration
ASP.NET Core 是如何读取配置文件,今天我们来学习。
ASP.NET Core的配置系统已经和之前版本的ASP.NET有所不同了,之前是依赖于System.Configuration和XML配置文件web.config。
新的配置系统支持多种格式的配置文件。
下面我们来以json 格式的配置文件正式开始学习。
我们新建一个ASP.NET Core Web 应用程序,选择无身份验证。

读取配置文件
在项目目录下有个 appsettings.json ,我们先来操作这个文件。
在appsettings.json 添加如下两个节点。

{
"Data": "LineZero",
"ConnectionStrings": {
"DefaultConnection": "数据库1",
"DevConnection": "数据库2"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}

下面我们来读取。由于项目默认已经将该文件加入ConfigurationBuilder 之中,所以我们可以直接来读取。
在 Configure 方法中读取:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
var data = Configuration["Data"];
//两种方式读取
var defaultcon = Configuration.GetConnectionString("DefaultConnection");
var devcon = Configuration["ConnectionStrings:DevConnection"];

调试程序,可以看到数据成功取出。
多环境区分
我们复制一个appsettings.json 然后重命名为 appsettings.Development.json
更改appsettings.Development.json 如下:

{
"Data": "LineZero Development",
"ConnectionStrings": {
"DefaultConnection": "开发数据库1",
"DevConnection": "开发数据库2"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}

然后我们调试程序,你会发现获取到的值变成了Development.json 里的值。

这里就是多环境配置。

public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)//增加环境配置文件,新建项目默认有
.AddEnvironmentVariables();
Configuration = builder.Build();
}

如果我们直接执行读取到的就是appsettings.json 的值,因为直接执行时是 Production 环境。
下面是输出图:
调试时:

dotnet run 时:

对象读取
我们在appsettings.json 及 Development.json 都添加一个 SiteConfig 节点。
"SiteConfig": {
"Name": "LineZero's Blog",
"Info": "ASP.NET Core 开发及跨平台,配置文件读取"
},
然后新建一个SiteConfig 类。
public class SiteConfig
{
public string Name { get; set; }
public string Info { get; set; }
}
首先在 ConfigureServices 中添加Options 及对应配置。

public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
//添加options
services.AddOptions();
services.Configure<SiteConfig>(Configuration.GetSection("SiteConfig"));
}

然后我们在 Controller 中读取。

public class HomeController : Controller
{
public SiteConfig Config; public HomeController(IOptions<SiteConfig> option)
{
Config = option.Value;
} public IActionResult Index()
{
return View(Config);
}
}

对应View Index.cshtml
@model SiteConfig
@{
ViewData["Title"] = Model.Name;
}
<h1>@Model.Name</h1>
<h2>@Model.Info</h2>
执行程序 http://localhost:5000/

NET Core开发-读取配置文件Configuration的更多相关文章
- ASP.NET Core开发-读取配置文件Configuration
ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配置系统已经和之前版本的ASP.NET有所不同了,之前是依赖于System.Configuration和XML ...
- ASP.NET Core开发-读取配置文件Configuration appsettings.json
https://www.cnblogs.com/linezero/p/Configuration.html ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配 ...
- .net core 学习 读取配置文件
在空项目中是没有配置文件的,首先要新建一个,配置文件内容如下,下面来读取各个内容 { "ConnectionStrings": { "DefaultConnection& ...
- .net core自定义读取配置文件
新建完成后项目目录下有个 appsettings.json { "Logging": { "LogLevel": { "Default": ...
- .net core 灵活读取配置文件
using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using Syst ...
- asp.net core mvc 读取配置文件appsettings.json
上一篇我们将了读取自定义配置文件.这篇我们讲一下asp.net core mvc里读取自带的配置文件 appsettings.json 首先创建个asp.net core mvc项目,项目里有Prog ...
- .net core中读取配置文件
1)先看丑陋的方法 读取 appsettings.json 然后在 Startup 的 ConfigureServices() 方法中进行注入: public IConfigurationRoot ...
- Spring Boot读取配置文件的几种方式
Spring Boot获取文件总的来说有三种方式,分别是@Value注解,@ConfigurationProperties注解和Environment接口.这三种注解可以配合着@PropertySou ...
- .NET Core类库中读取配置文件
最近在开发基于.NET Core的NuGet包,遇到一个问题:.NET Core中已经没有ConfigurationManager类,在类库中无法像.NET Framework那样读取App.conf ...
随机推荐
- weblogic上部署应用程序
weblogic上部署应用程序有三种方法: 一:修改配置文件config.xml在文件中加入如下代码片段: <app-deployment> <name>FAB</nam ...
- 常用工具类,文件和内存的大小获取,shell脚本的执行
/* * Copyright (C) 2012 The Android Open Source Project * * Licensed under the Apache License, Versi ...
- jQuery 方法
方法 描述 animate() 对被选元素应用"自定义"的动画 clearQueue() 对被选元素移除所有排队函数(仍未运行的) delay() 对被选元素的所有排队函数(仍未运 ...
- PendingIntent概述
一.定义 PendingIntent表示待定.等待.即将发生的意思.Intent表示的是立刻发生. PendingIntent的主要方法: int requestCode:表示请求码,跟intent是 ...
- ie 6 position fixed
IE6position:fixed问题解决方案 2013-11-06 18:25:04| 分类: JS/CSS | 标签:ie6 fixed |举报 |字号大中小订阅 普通写法 #to ...
- qq视频api代码
<!--视频容器--> <div id="mod_player"></div> <!--腾讯视频代码开始--> <scri ...
- hdu 4521 线段树改点求点的应用
小明系列问题——小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- iOS 四种延时的方法
- (void)initBlock{ //延时的方法 //1:GCD延时 此方式在能够在參数中选择运行的线程. 是一种非堵塞的运行方式,没有找到取消运行的方法. double ...
- android 实现自己定义状态栏通知(Status Notification)
在android项目的开发中,有时为了实现和用户更好的交互,在通知栏这一小小的旮旯里,我们通常须要将内容丰富起来,这个时候我们就须要去实现自己定义的通知栏,比如以下360或者网易的样式: 首先我们要了 ...
- HDU2054_A == B ?【模拟题】【大数】【水的问题】
A == B ? Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...