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的更多相关文章

  1. ASP.NET Core开发-读取配置文件Configuration

    ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配置系统已经和之前版本的ASP.NET有所不同了,之前是依赖于System.Configuration和XML ...

  2. ASP.NET Core开发-读取配置文件Configuration appsettings.json

    https://www.cnblogs.com/linezero/p/Configuration.html ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配 ...

  3. .net core 学习 读取配置文件

    在空项目中是没有配置文件的,首先要新建一个,配置文件内容如下,下面来读取各个内容 { "ConnectionStrings": { "DefaultConnection& ...

  4. .net core自定义读取配置文件

    新建完成后项目目录下有个 appsettings.json { "Logging": { "LogLevel": { "Default": ...

  5. .net core 灵活读取配置文件

    using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using Syst ...

  6. asp.net core mvc 读取配置文件appsettings.json

    上一篇我们将了读取自定义配置文件.这篇我们讲一下asp.net core mvc里读取自带的配置文件 appsettings.json 首先创建个asp.net core mvc项目,项目里有Prog ...

  7. .net core中读取配置文件

    1)先看丑陋的方法 读取 appsettings.json   然后在 Startup 的 ConfigureServices() 方法中进行注入: public IConfigurationRoot ...

  8. Spring Boot读取配置文件的几种方式

    Spring Boot获取文件总的来说有三种方式,分别是@Value注解,@ConfigurationProperties注解和Environment接口.这三种注解可以配合着@PropertySou ...

  9. .NET Core类库中读取配置文件

    最近在开发基于.NET Core的NuGet包,遇到一个问题:.NET Core中已经没有ConfigurationManager类,在类库中无法像.NET Framework那样读取App.conf ...

随机推荐

  1. java学习:AWT组件和事件处理的笔记(1)--文本框上的ActionEvent事件

    学习处理事件时,必须很好的掌握事件源,监视器,处理事件的接口    1.事件源        能够产生java认可事件的对象都可称为事件源,也就是说事件源必须是对象    2.监视器        监 ...

  2. docker 使用Data Volume 共享文件

    Adding a data volume You can add a data volume to a container using the -v flag with the docker run  ...

  3. MVC 视频笔记

    1.关闭Jquery的浏览器缓存 $.ajaxSetup({cache:fasle});

  4. 如何让旧版IE浏览器认识HTML5元素

    <!--[if lt IE 9]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js" ...

  5. css 优先级

    css优先级的四大原则: 原则一: 继承不如指定 如果某样式是继承来的永远不如具体指定的优先级高.例子1:CODE:<style type="text/css"> &l ...

  6. Java缓存

    Java中要用到缓存的地方很多,首当其冲的就是持久层缓存,针对持久层谈一下: 要实现java缓存有很多种方式,最简单的无非就是static HashMap,这个显然是基于内存缓存,一个map就可以搞定 ...

  7. linux 用户空间获得纳秒级时间ns

    一.引言 我们在测试程序的性能的时候往往需要获得ns级的精确时间去衡量一个程序的性能,下面介绍下linux中用户空间获得ns级时间的方法 二.用户空间获得ns级时间 使用clock_gettime函数 ...

  8. ACM计算几何题目推荐

    //第一期 计算几何题的特点与做题要领: 1.大部分不会很难,少部分题目思路很巧妙 2.做计算几何题目,模板很重要,模板必须高度可靠. 3.要注意代码的组织,因为计算几何的题目很容易上两百行代码,里面 ...

  9. 数据结构multiset hdu-2275-Kiki & Little Kiki 1

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2275 题目意思: 有两种操作: 1.push a 把a放进数组里. 2.pop a 输出不超过a的最 ...

  10. OC基础4:类和方法

    "OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1.类的声明(@interface)要放在 ...