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 程 ...
随机推荐
- Netty 系列目录
Netty 系列目录 二 Netty 源码分析(4.1.20) 1.1 Netty 源码(一)Netty 组件简介 2.1 Netty 源码(一)服务端启动 2.2 Netty 源码(二)客户端启动 ...
- jetty无法即时更新html、js、css等静态文件的解决办法
网上搜了一大堆方法.最常见的是找到jetty\etc\webdefault.xml文件,找到useFileMappedBuffer参数,把true改为false <init-param> ...
- java实现从url路径中下载pdf文档到本地
package com.cellstrain.icell.util; import java.io.*;import java.net.*; public class DownloadPdf { /* ...
- 2018.09.20 atcoder Building Cubes with AtCoDeer(枚举)
传送门 有个十分显然的结论,只用枚举前后两个面就可以知道所有的面的颜色. 于是可以O(n2)O(n^2)O(n2)枚举前后两个面然后用map乱搞求贡献. 发现这样算出来会多算两倍(打表证明)于是答案除 ...
- try-catch+thows异常范围说明
方式一: CatalogPO deleteTarget = null; /** 查询是否存在 **/ deleteTarget = catalogMapper.findByCatalogId(cata ...
- Spring MVC之@RequestMapping 传递数组
1.前台: var param = {titles:['col1','col2','col3']}; $.ajax({url:url, type:"post", data:para ...
- 基础的linux学习
学习了这几个命令分享一下: 文本文件内搜索数据 grep -n -e pattern1 -e pattern2 file1 -n 搜索到的数据显示行号展示 -e pattern1 多个匹配模式下可以通 ...
- TurtleBot教程
TurtleBot TurtleBot combines popular off-the-shelf robot components like the iRobot Create, Yujin Ro ...
- (博弈)Simple Game --codeforces--570B
链接: http://codeforces.com/problemset/problem/570/B http://acm.hust.edu.cn/vjudge/contest/view.action ...
- CDialog类
CDilalog包含三个关键函数:OnInitDialog.OnOK和OnCancel,可以覆盖这三个函数初始化对话框.响应点击OK和Cancel按钮.尽管每个函数都响应一条对话框消息,但是不需要你提 ...