ASP.NET 8 配置文件

IConfiguration

1、在appsettings.json文件添加配置

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "Id": "123456",
  "Name": "KK老师",
  "TechInfo": {
    "Id": 123456,
    "Name": "KK001"
  },
  "ConnectionStrings": {
    "WriteConnection": "Server=LAPYUIU(DEJP1;DataBase=demo1;Trusted_Connection=True;",
    "ReadConnectionList": [
      "Server=LAPYUIU(DEJP1;DataBase=demo01;Trusted_Connection=True;",
      "Server=LAPYUIU(DEJP1;DataBase=demo02;Trusted_Connection=True;",
      "Server=LAPYUIU(DEJP1;DataBase=demo03;Trusted_Connection=True;"
    ]
  },
  "iocConfig": "IOCPTOP-JU"
}

2、获取配置文件中配置信息三大方法

1)常规

Program.cs中:

string? id = builder.Configuration["Id"];
string? name = builder.Configuration.GetValue<string>("Name");

Controller中:

private readonly IConfiguration _config;

public A02ConfigController(IConfiguration config)
{
    _config = config;
}

public IActionResult Index()
{
    string? id = _config.GetValue<string>("id");
    string? name = _config["Name"];
    return View(id);
}

2)新建类并绑定

根据下面配置文件内容新建类:

"ConnectionStrings": {
    "WriteConnection": "Server=LAPYUIU(DEJP1;DataBase=demo1;Trusted_Connection=True;",
    "ReadConnectionList": [
      "Server=LAPYUIU(DEJP1;DataBase=demo01;Trusted_Connection=True;",
      "Server=LAPYUIU(DEJP1;DataBase=demo02;Trusted_Connection=True;",
      "Server=LAPYUIU(DEJP1;DataBase=demo03;Trusted_Connection=True;"
    ]
  },

ConnectionStrings.cs

public class ConnectionStrings
{
    public string? WriteConnection {  get; set; }    
    public List<string>? ReadConnectionList { get; set; }
}

得到配置信息

//Programs.cs中
//ConnectionStrings options = new ConnectionStrings();
//builder.Configuration.Bind("ConnectionStrings", options);

ConnectionStrings options = new ConnectionStrings();
_config.Bind("ConnectionStrings", options);
string? s = options.WriteConnection;
string? s1 = options.ReadConnectionList[0];
string? s2 = options.ReadConnectionList[1];
string? s3 = options.ReadConnectionList[2];

3)通过IServiceCollection配置, Program.cs中自动完成配置信息映射到和配置文件相同格式的实体中去

同2)需要先新建类ConnectionStrings.cs

然后再Program.cs中添加如下代码:

builder.Services.Configure<ConnectionStrings>(builder.Configuration.GetSection("ConnectionStrings"));

Controller中使用(IOptionsMonitor和IOptions均可以使用):

private readonly ConnectionStrings _ConnectionStrings;
private readonly ConnectionStrings _Options;

public A02ConfigController(IOptionsMonitor<ConnectionStrings> optionsMonitor, IOptions<ConnectionStrings> options)
{
    _ConnectionStrings = optionsMonitor.CurrentValue;
    _Options = options.Value;
}

public IActionResult Index()
{
    string? s1 = _Options.WriteConnection;
    List<string>? l1 = _ConnectionStrings.ReadConnectionList;
    return View();
}

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄健华Yeah

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值