.netcore获取appsettings.json配置

1.文件中配置

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "JWT": {
    "Issuer": "PaperMask",
    "Audience": "Audience",
    "IssuerSigningKey": "PaperMask007",
    "AccessTokenExpiresMinutes": "30"
  },
  "ConnectionStrings": {
    "Conn": "Host=.;Database=TestDb;User ID=sa;Password=123456;"
  }
}

2.单个获取配置文件方法

Configuration.GetConnectionString("Conn");

Configuration["ConnectionStrings:Conn"];

3.多个获取配置文件方法
添加配置对应的cs文件

public class JWTConfig
{
    public string Issuer { get; set; }
    public string Audience { get; set; }
    public string IssuerSigningKey { get; set; }
    public double AccessTokenExpiresMinutes { get; set; }
}

通过管道注册

services.Configure<JWTConfig>(Configuration.GetSection("JWT"));//.AddMvc();
或
JWTConfig config = new JWTConfig();
Configuration.GetSection("JWT").Bind(config);
posted @ 2019-12-25 14:03  纸面具  阅读(606)  评论(0编辑  收藏  举报