将json文件的配置转换成C#的实体

新建项目:

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:等线;
mso-ascii-font-family:等线;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:等线;
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:等线;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

OptionsBindSample

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:等线;
mso-ascii-font-family:等线;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:等线;
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:等线;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

忘了把 https去掉了。下次记住了 就可以了。

注入Configuration

由于我们要用Configuration所以要用到依赖注入。

我们可以使用IConfiguration的接口。

注意引入命名空间:

using Microsoft.Extensions.Configuration;

这里调用configuration的bind方法。可以传入一个对象将我们配置信息和对象映射起来。

新建Class的班级类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace OptionsBindSample
{
public class Class
{
public int ClassNo { get; set; }
public string ClassDesc { get; set; }
public List<Student> Students { get; set; }
}
public class Student
{
public string Name { get; set; }
public string Age { get; set; }
}
}

Class.cs

新建appsetting.json文件

我们在新建项目的时候,默认自带了这个appsettings.json文件了。

默认webhost启动的时候是没有添加任何配置和启动相关的

我们默认host启动会读取appsetting.json读取我们的configuration里面去。

只要你的名字叫做appsettings.json

它会通过CreateDefaultBuilder这个方法,会把它加载到我们的configuration里面

把之前的json文件拷贝过去

{
"ClassNo": "",
"ClassDesc": "ASP.NET Core 101", "Students": [
{
"name": "jesse",
"age": ""
},
{
"name": "jim",
"age": ""
},
{
"name": "lisa",
"age": ""
}
]
}

json

我们使用控制台的方式启动项目

输出结果

ClassNo:1ClassDesc:ASP.NET Core 101ClassDesc:

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:等线;
mso-ascii-font-family:等线;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:等线;
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:等线;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

任务12:Bind读取配置到C#实例的更多相关文章

  1. 【ASP.NET Core快速入门】(五)命令行配置、Json文件配置、Bind读取配置到C#实例、在Core Mvc中使用Options

    命令行配置 我们通过vs2017创建一个控制台项目CommandLineSample 可以看到现在项目以来的是dotnet core framework 我们需要吧asp.net core引用进来,我 ...

  2. 菜鸟入门【ASP.NET Core】5:命令行配置、Json文件配置、Bind读取配置到C#实例、在Core Mvc中使用Options

      命令行配置 我们通过vs2017创建一个控制台项目CommandLineSample 可以看到现在项目以来的是dotnet core framework 我们需要吧asp.net core引用进来 ...

  3. Bind读取配置到C#实例

    1.创建一个空的ASP.NET Core Web 应用程序 2.程序包管理控制台执行Install-Package Microsoft.AspNetCore -Version 2.0.1 3.创建js ...

  4. (十二)Bind读取配置到C#实例

    继续上一节的,接下来用Options或者Bind把json文件里的配置转成C#的实体,相互之间映射起来.首先新建一个asp.net core mvc项目OptionsBindSample Startu ...

  5. 使用Bind读取配置到C#的实例

    在之前的一篇二级域名绑定的文章<.Net Core 二级域名绑定到指定的控制器>中,有一个小的地方是关于读取Json文件的配置信息的,当时是用了读取文件流的方式,一直以来觉得该方法太Low ...

  6. Bind,Options读取配置到C#实例

    首先创建一个网站 Asp.net Core Mvc 空网站 起名叫做OptionsBindSample 通过Option和Bind将Json文件里面的配置转成C#里面的一个实体,相互之间映射起来 Bi ...

  7. ASP.NET Core轻松入门Bind读取配置文件到C#实例

    首先新建一个ASP.NET Core空项目,命名为BindReader 然后 向项目中添加一个名为appsettings.json的json文件,为什么叫appsettings呢?  打开Progra ...

  8. asp.net core-6.Bind读取配置文件到C#实例中

    1,创建asp.net core web应用程序,选择空网站 2,创建一个appsettings.json文件 为什么要叫appsettings.json呢?因为在Iwebhost启动的时候没有添加任 ...

  9. Bind安装配置及应用

    Bind安装配置及应用 BIND:Berkeley Internet Name Domain ,ISC.org     DNS服务的实现:     监听端口:53/UDP , 53/TCP     程 ...

随机推荐

  1. Action Bar详解(二)

    在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果. 一.添加A ...

  2. Codeforces Round #266 (Div. 2) C. Number of Ways

    You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split ...

  3. 关于 thinkPHP Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback

    Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback 关于thinkPHP rpc调 ...

  4. hdu 4707 Pet(DFS &amp;&amp; 邻接表)

    Pet Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  5. C++经典面试题解析

    1. // BlankTest.cpp : 定义控制台应用程序的入口点. //题目:将一个文件中的一组整数排序后输出到另一个文件中 #include "stdafx.h" #inc ...

  6. [LightOJ 1018]Brush (IV)[状压DP]

    题目链接:http://lightoj.com/volume_showproblem.php? problem=1018 题意分析:平面上有不超过N个点,如今能够随意方向划直线将它们划去,问:最少要划 ...

  7. numpy - 数组索引

    numpy 数组索引 一.单个元素索引 一维数组索引 >>> x = np.arange(10) >>> x[2] 2 >>> x[-2] 8 二 ...

  8. (C/C++)register关键字

    register:这个关键字的作用是请求编译器尽可能的将变量存在CPU内部寄存器中,而不是通过内存寻址访问,以提高效率. 注意是尽可能,不是绝对.一个CPU 的寄存器也就那么几个或几十个,你要是定义了 ...

  9. nginx、mysql、php等各编译参数查询

    查看nginx编译参数:/usr/local/nginx/sbin/nginx -V 查看apache编译参数:cat /usr/local/apache2/build/config.nice 查看m ...

  10. Scrapy运行报错:ModuleNotFoundError: No module named 'douban.douban'

    运行scrapy爬虫报错: from douban.douban.items import DoubanItem ModuleNotFoundError: No module named 'douba ...