标签:action .net index lap opened ogr etc isp close
一、配置管理
1,读取内存配置
using System; using Microsoft.Extensions.Configuration; using System.Collections.Generic; namespace ConsoleApp1 { class Program { static void Main(string[] args) { Dictionary<string, string> dic = new Dictionary<string, string>() { { "name","hunter"}, { "age","10"} }; var builder = new ConfigurationBuilder() .AddInMemoryCollection(dic)//当age没有值的时候使用dic里面的值 .AddCommandLine(args); var configuration = builder.Build(); Console.WriteLine($"name:{configuration["name"]}"); Console.WriteLine($"age:{configuration["age"]}"); Console.ReadKey(); } } }
2,读取json文件
using System; using Microsoft.Extensions.Configuration; using System.Collections.Generic; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var builder = new ConfigurationBuilder() .AddJsonFile("class.json"); var configuration = builder.Build(); Console.WriteLine($"no:{configuration["no"]}"); Console.WriteLine($"name:{configuration["name"]}"); Console.WriteLine("student:"); Console.WriteLine($"no:{configuration["student:0:no"]},name:{configuration["student:0:name"]}"); Console.WriteLine($"no:{configuration["student:1:no"]},name:{configuration["student:1:name"]}"); Console.ReadKey(); } } }
{ "no": "1", "name": "asp.net core", "student": [ { "no": "1", "name": "张三" }, { "no": "2", "name": "张三" } ] }
3,读取appsettings.json
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; namespace WebApplication1.Controllers { public class HomeController : Controller { private IConfiguration _configuration; public HomeController(IConfiguration configuration) { _configuration = configuration; } public IActionResult Index() { Class c = new Class(); _configuration.Bind(c); return View(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace WebApplication1 { public class Class { public string no { get; set; } public string name { get; set; } public IEnumerable<student> student { get; set; } } public class student { public string no { get; set; } public string name { get; set; } } }
{ "no": "1", "name": "asp.net core", "student": [ { "no": "1", "name": "张三" }, { "no": "2", "name": "张三" } ] }
标签:action .net index lap opened ogr etc isp close
原文地址:https://www.cnblogs.com/zd1994/p/8987200.html