• 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 };
    }
  • 相关阅读:
    课后作业
    大道至简第六章读后感
    Ljava.lang.Object;@ba8a1dc
    课后作业
    大道至简第五章读后感
    课后作业加密
    动手动脑
    大道至简第四章读后感
    NEU 解题报告索引
    Aizu 解题报告索引
  • 原文地址:https://www.cnblogs.com/come-on-come-on/p/12083016.html
Copyright © 2020-2023  润新知