依赖注入之IConfiguration 访问appSetting.json
(1)创建NET6 Webapi
(2)appsettings.json
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "AppSettings": { //单库 "ConnectionString": "Server=cnps-mis-yfw;User Id=sa;Password=123;Database=northwind;" }, "DBS": [ //多库(数组) { "dbid": "0", "conn": "Server=cnps-mis-yfw;User Id=sa;Password=123;Database=northwind1;" }, { "dbid": "1", "conn": "Server=cnps-mis-yfw;User Id=sa;Password=456;Database=northwind2;" }, { "dbid": "2", "conn": "Server=cnps-mis-yfw;User Id=sa;Password=789;Database=northwind3;" } ], "Settings": { //对象 "Name": "test123", "IsValid": true, "Counter": 123 }, "MulConn": { "code": "200", "msg": "ok", "data": [ { "dbid": "0", "conn": "Server=cnps-mis-yfw;User Id=sa;Password=123;Database=northwind1;" }, { "dbid": "1", "conn": "Server=cnps-mis-yfw;User Id=sa;Password=456;Database=northwind2;" }, { "dbid": "2", "conn": "Server=cnps-mis-yfw;User Id=sa;Password=789;Database=northwind3;" } ] } }
(3)program.cs
IConfiguration configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .Build(); var builder = WebApplication.CreateBuilder(args); //builder.Services.AddSingleton(configuration); //或 builder.Services.AddSingleton<IConfiguration>(configuration); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run();
(4)UserController.cs
//using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; //using Microsoft.Extensions.Configuration; //using static System.Collections.Specialized.BitVector32; //using Microsoft.Extensions.Configuration.Json; //using static webapi02.Controllers.UserController; namespace webapi02.Controllers { [Route("api/[controller]")] [ApiController] public class UserController : ControllerBase { private readonly IConfiguration _configuration; public UserController(IConfiguration config) { _configuration = config; } [HttpGet("[action]")] public IActionResult SayHello() { //(01) string? connstr = _configuration["AppSettings:ConnectionString"]; string? connstr2 = _configuration.GetValue<string>("AppSettings:ConnectionString"); //结果:Server=cnps-mis-yfw;User Id=sa;Password=123;Database=northwind; //(02) List<dbconfig> list1 = new List<dbconfig>(); _configuration.Bind(string.Join(":", new string[] { "DBS" }), list1); //或 List<dbconfig> list2 = new List<dbconfig>(); _configuration.Bind(string.Join(":", new string[] { "MulConn", "data" }), list2); //(03) var list3 = new List<dbconfig>(); _configuration.GetSection("MulConn:data").Bind(list3); //或 var list4 = new List<dbconfig>(); _configuration.GetSection("MulConn").Bind("data",list4); //(04) var settings = new Settings(); _configuration.GetSection("Settings").Bind(settings); return new JsonResult(list1); //return new JsonResult(settings); //list1~list4结果: //[ // { // "dbid": "0", // "conn": "Server=cnps-mis-yfw;User Id=sa;Password=123;Database=northwind1;" // }, // { // "dbid": "1", // "conn": "Server=cnps-mis-yfw;User Id=sa;Password=456;Database=northwind2;" // }, // { // "dbid": "2", // "conn": "Server=cnps-mis-yfw;User Id=sa;Password=789;Database=northwind3;" // } //] //settings结果 //{ // "name": "test123", // "isValue": false, // "counter": 123 //} } public class dbconfig { public string dbid { get; set; } public string conn { get; set; } } public class Settings { public string Name { get; set; } public bool IsValue { get; set; } public int Counter { get; set; } } } }
(5)结果