(十二)Bind读取配置到C#实例
2021-04-09 19:28
标签:service col style ogr 依赖 pre settings add mic (十二)Bind读取配置到C#实例 标签:service col style ogr 依赖 pre settings add mic 原文地址:https://www.cnblogs.com/dotnetHui/p/9058102.html
1 public IConfiguration Configuration { get; set; }
2
3 public Startup(IConfiguration configuration)
4 {
5 this.Configuration = configuration;
6 }
1 public class Class
2 {
3 public int ClassNo { get; set; }
4 public string ClassDesc { get; set; }
5 public List
1 {
2 "ClassNo": "1",
3 "ClassDesc": "ASP.NET Core 101",
4 "Students": [
5 {
6 "name": "liuxh",
7 "age": "31"
8 },
9 {
10 "name": "linhj",
11 "age": "31"
12 },
13 {
14 "name": "liuxy",
15 "age": "7"
16
17
18
19 },
20 {
21 "name": "liuss",
22 "age": "1"
23 }
24 ]
25
26 }
public IConfiguration Configuration { get; set; }
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();
}
app.Run(async (context) =>
{
var myClass = new Class();
Configuration.Bind(myClass);//把配置文件的信息和对象映射起来
await context.Response.WriteAsync($"ClassNo:{myClass.ClassNo}");
await context.Response.WriteAsync($"ClassDesc:{myClass.ClassDesc}");
await context.Response.WriteAsync($"{myClass.Students.Count} Students");
});
}
上一篇:window中常用的命令
下一篇:【C#复习总结】细说匿名方法