Bind读取配置到C#实例
1.创建一个空的ASP.NET Core Web 应用程序
2.程序包管理控制台执行Install-Package Microsoft.AspNetCore -Version 2.0.1
3.创建json文件命名为:appsettings.json,再添加一个Class类
appsettings.json内容为:
{
"ClassNo": "",
"ClassDesc": "ASP.NET Core 101",
"Students": [
{
"name": "name1",
"age": ""
},
{
"name": "name2",
"age": ""
},
{
"name": "name13",
"age": ""
}
]
}
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; }
}
}
4.Startup.cs 编写读取配置构造方法,并在app.Run方法打印,代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration; namespace OptionsBindSample
{
public class Startup
{
/// <summary>
/// 添加构造方法用来获取配置信息
/// </summary>
public IConfiguration Configuration { get; set; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} // This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} app.Run(async (context) =>
{
var myClass = new Class();
Configuration.Bind(myClass);//绑定基础信息
//输出json配置到页面
await context.Response.WriteAsync($"ClassNo {myClass.ClassNo}");
await context.Response.WriteAsync($"ClassDesc {myClass.ClassDesc}");
await context.Response.WriteAsync($"myClass Count {myClass.Students.Count}");
});
}
}
}
5.启动程序,输出结果

Bind读取配置到C#实例的更多相关文章
- 【ASP.NET Core快速入门】(五)命令行配置、Json文件配置、Bind读取配置到C#实例、在Core Mvc中使用Options
命令行配置 我们通过vs2017创建一个控制台项目CommandLineSample 可以看到现在项目以来的是dotnet core framework 我们需要吧asp.net core引用进来,我 ...
- 菜鸟入门【ASP.NET Core】5:命令行配置、Json文件配置、Bind读取配置到C#实例、在Core Mvc中使用Options
命令行配置 我们通过vs2017创建一个控制台项目CommandLineSample 可以看到现在项目以来的是dotnet core framework 我们需要吧asp.net core引用进来 ...
- 任务12:Bind读取配置到C#实例
将json文件的配置转换成C#的实体 新建项目: Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE /* Style Definition ...
- (十二)Bind读取配置到C#实例
继续上一节的,接下来用Options或者Bind把json文件里的配置转成C#的实体,相互之间映射起来.首先新建一个asp.net core mvc项目OptionsBindSample Startu ...
- 使用Bind读取配置到C#的实例
在之前的一篇二级域名绑定的文章<.Net Core 二级域名绑定到指定的控制器>中,有一个小的地方是关于读取Json文件的配置信息的,当时是用了读取文件流的方式,一直以来觉得该方法太Low ...
- Bind,Options读取配置到C#实例
首先创建一个网站 Asp.net Core Mvc 空网站 起名叫做OptionsBindSample 通过Option和Bind将Json文件里面的配置转成C#里面的一个实体,相互之间映射起来 Bi ...
- ASP.NET Core轻松入门Bind读取配置文件到C#实例
首先新建一个ASP.NET Core空项目,命名为BindReader 然后 向项目中添加一个名为appsettings.json的json文件,为什么叫appsettings呢? 打开Progra ...
- asp.net core-6.Bind读取配置文件到C#实例中
1,创建asp.net core web应用程序,选择空网站 2,创建一个appsettings.json文件 为什么要叫appsettings.json呢?因为在Iwebhost启动的时候没有添加任 ...
- Bind安装配置及应用
Bind安装配置及应用 BIND:Berkeley Internet Name Domain ,ISC.org DNS服务的实现: 监听端口:53/UDP , 53/TCP 程 ...
随机推荐
- ubuntu关闭防火墙
https://jingyan.baidu.com/article/73c3ce283ee2c1e50343d9f6.html
- QFileInfo
https://www.cnblogs.com/findumars/p/10247573.html
- 将C语言宏定义数值转换成字符串!
将C语言宏定义转换成字符串! 摘自:https://blog.csdn.net/happen23/article/details/50602667 2016年01月28日 19:15:47 六个九十度 ...
- js 和 css 中 不能使用 jsp 页面中一些 标签 和 java 代码等,应注意
js 和 css 中 不能使用 jsp 页面中一些 标签 和 java 代码等,应注意 如 ${ } <%%> 等
- 2018.09.07 Amount of degrees(数位dp)
描述 求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的B的整数次幂之和. 例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足题意: 17 = 24+20, ...
- Django入门指南-第8章:第一个测试用例(完结)
python manage.py test python manage.py test --verbosity=2 # boards/tests.py from django.core.urlreso ...
- lynis-*nix安全审计
cd /usr/local/lynis ./lynis --man 全部检查: ./lynis --check-all -Q 采用crontab自动检查: ./lynis -c --auditor & ...
- org.springframework spring-test
需要的jar包 <dependency> <groupId>org.springframework</groupId> <artifactId>spri ...
- 伪共享(False Sharing)
原文地址:http://ifeve.com/false-sharing/ 作者:Martin Thompson 译者:丁一 缓存系统中是以缓存行(cache line)为单位存储的.缓存行是2的整数 ...
- 几个经典的数学库之一学习---VCGlib(1)
1. VCG Libary是Visulization and Computer Graphics Libary(可视化与计算机图形学库)的缩写,是一个开源的C++模板库,用于三角网格和四面体网格的控制 ...