• NetCore配置文件读取


    读取appSetting文件

    配置文件如下

    {
      "Logging": {
        "LogLevel": {
          "Default": "Warning"
        }
      },
      "AllowedHosts": "*",
      "WebSetting": {
        "WebName": "ASP.NET Core",
        "Title": "Hello Title",
        "Behavior": {
          "IsCheckIp": true,
          "MaxConnection": 300
         }
       }
    }

    方法一、键读取

    首先,在类构造函数注入  IConfiguration,例如:

     public IConfiguration Configuration { get; }
     public ValuesController(IConfiguration configuration)
     {
         Configuration = configuration;
     }

    然后就可以在该类任何方法读取:

    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
       var title = Configuration["WebSetting:Title"];
       var isCheckIp = Configuration["WebSetting:Behavior:IsCheckIp"];
       return new string[] { title, isCheckIp };
    }

    方法二、绑定读取

    首先,还是类构造函数注入ICofiguration

    public IConfiguration Configuration { get; }
    public ValuesController(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    然后,创建配置项结构对应的配置模型

    public class Behavior
    {
        public bool IsCheckIp { get; set; }
        public int MaxConnection { get; set; }
    }
    
    public class WebSetting
    {
         public string WebName { get; set; }
         public string Title { get; set; }
         public Behavior Behavior { get; set; }
    }

    最后如下方式读取配置

    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
       var webSetting = new WebSetting();
       Configuration.GetSection("WebSetting").Bind(webSetting);
       var webName = webSetting.WebName;
       var isCheckIp = webSetting.Behavior.IsCheckIp;
       return new string[] { webName, isCheckIp.ToString() };
    }

    方法三、注册读取

    首先,创建配置项结构对应的配置模型

    public class Behavior
    {
        public bool IsCheckIp { get; set; }
        public int MaxConnection { get; set; }
    }
    
    public class WebSetting
    {
         public string WebName { get; set; }
         public string Title { get; set; }
         public Behavior Behavior { get; set; }
    }

    然后,在 ConfigureServices 方法中注册

     public void ConfigureServices(IServiceCollection services)
    {
              
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        //注册配置
        services.Configure<WebSetting>(Configuration.GetSection("WebSetting"));
    }

    最后读取配置:在读取配置的类构造函数注入IOptions,就可以在该类任何位置读取配置如下

    1、构造函数注入读取

    private IOptions<WebSetting> webSettingOption;
    //构造函数
    public ValuesController(IOptions<WebSetting> _webSettingOption)
    {
        webSettingOption = _webSettingOption;
    }
    
           
    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
         var webName = webSettingOption.Value.WebName;
         var isCheckIp = webSettingOption.Value.Behavior.IsCheckIp;
         return new string[] { webName, isCheckIp.ToString() };
    }

    自定义配置文件

    首先新建配置文件,名称customSetting.json,并右击该文件选择属性进行设置,设置复制到输出目录:始终复制

    {
      "Name": "Rick",
      "Age": 99,
      "TicketSetting": {
        "BaseHost": "www.baidu.com",
        "Port": 9950
      }
    }

    然后定义配置文件结果类

    public class CustomSetting
    {
      public string Name { get; set; }
    
      public int Age { get; set; }
    
      public TicketSetting TicketSetting { get; set; }
    }
    
    public class TicketSetting
    {
      public string BaseHost { get; set; }
      public int Port { get; set; }
    }

    其次ConfigureServices方法进行配置注册

    public void ConfigureServices(IServiceCollection services)
    {
          services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    
          var config = new ConfigurationBuilder()
                    .AddJsonFile("customSetting.json")
                    .Build();
          services.Configure<CustomSetting>(config);
    }

    最后读取

    private IOptions<CustomSetting> _option;
    public ValuesController(IOptions<CustomSetting> option)
    {
                _option = option;
    }
    
    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        var host = _option.Value.TicketSetting.BaseHost;
        return new string[] { host };
    }
  • 相关阅读:
    gitlab 升级
    通过 备份文件 恢复/迁移 gitlab
    gitlab 远程 定时备份
    PHP 中替换若干字符串字串为数组中的值,不用循环,非常高效
    php 在客户端生成txt文件并且下载
    Mysql时间戳函数
    mysql中在表中insert数据时,有重复主键id时,变成update
    css中解决img在a元素中使得a元素撑大写法
    php以excel的形式将数据输出
    css中常用的hack
  • 原文地址:https://www.cnblogs.com/come-on-come-on/p/12083016.html
Copyright © 2020-2023  润新知