1-创建一个空的dotnet mvc网站

2- 创建appsettings.json文件, 这文件会默认被绑定

{
"ClassNo": "1",
"ClassDesc": "Asp.net core",
"Students": [
{
"name": "lili",
"age": "11"
},
{
"name": "xiaofeng",
"age": "33"
},
{
"name": "xiaobao",
"age": "66"
}
]
}

  3-创建一个可映射的class类

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

4-开始绑定

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 optionBindingDemo
{
public class Startup
{
IConfiguration Configuration;
public Startup(IConfiguration configuration)
{
this.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();
}
Classes cls = new Classes();
this.Configuration.Bind(cls);
app.Run(async (context) =>
{
await context.Response.WriteAsync($"ClassNo: {cls.ClassNo}");
await context.Response.WriteAsync($"ClassDesc: {cls.ClassDesc}");
await context.Response.WriteAsync($"studentListCount: {cls.Students.Count}");
});
}
}
}

最新文章

  1. MVC发布后项目存在于根目录中的子目录中时的css与js、图片路径问题
  2. JavaScript实现计算两个日期之间的天数
  3. mm/swap
  4. NoClassDefFoundError:aspectj/weaver/reflect/ReflectionWorld$Reflection
  5. 一个关于正整数x的约数个数的结论
  6. CentOS-6.5安装配置Tomcat7
  7. struct 和 class 不同点
  8. gunicorn syncworker 源码解析
  9. Docker最全教程之使用Docker搭建Java开发环境(十七)
  10. C# linq语句学习
  11. 使用jQuery插件时避免重复引入jquery.js文件
  12. AtCoder Regular Contest 094 (ARC094) CDE题解
  13. python——描述符
  14. SD从零开始33-37
  15. centos 搭建php环境
  16. linux下mysql安装和调优
  17. HDU——1573 X问题
  18. 第五章:异步Web服务
  19. 如何创建systemd定时任务
  20. 使用springmvc时报错JSPs only permit GET POST or HEAD

热门文章

  1. 在页面rem布局中用到的js代码
  2. nginx配置优化-生产环境应用版
  3. jquery对radio的操作汇总
  4. 什么是permit-inside功能
  5. Ubuntu 如何将桌面上的Home中的文件夹除去
  6. Gym 100090M Jumping along the Hummocks
  7. 最小堆的维护,POJ(2051)
  8. node入口文件分析和目录初始化
  9. ASP.NET中刷新分页
  10. C#继承的多态性