zoukankan      html  css  js  c++  java
  • webapi读取配置文件内容

    1.读取默认的配置文件appsettings.json

    appsettings.json内容
    {
    "Logging": {"Default": "Information", "Microsoft": "Warning", }, "AllowedHosts": "*", "ConnectionString": "Server=.;Database=Carina;User ID=sa;Password=pass****;" }
    public class WeatherForecastController : ControllerBase
      {
        private IConfiguration _configuration;
    
        private readonly ILogger<WeatherForecastController> _logger;
      //依赖注入configuration,就可以在controller中使用configuration了
        public WeatherForecastController(ILogger<WeatherForecastController> logger, IConfiguration configuration)
        {
          _logger = logger;
          _configuration = configuration;  //
        }
      //读取单个值
      var connectionStr =
    _configuration["ConnectionString"] ;
      //var connectionStr =_configuration.GetValue<string>("ConnectionString")
      //获取对象中的值
      var
    connectionStr =_configuration["Logging:Default"] //返回”Information“
    }

    若要读出一个对象,需要先声明一个类,然后读取出来。若要依赖注入该配置文件对象,需要再startup类中声明

    public class Logging{
          public string Default{get;set;},
          public string Microsoft{get;set}
    }

    //controller类中
    var config = _configuration.GetSection("logging1").Get<Logging>();

    //若要依赖注入配置文件的对象。 //startup类 public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.Configure<Logging>(Configuration.GetSection("Logging")); } //controller类中 private IConfiguration _configuration; private Logging logging; private readonly ILogger<WeatherForecastController> _logger; public WeatherForecastController(ILogger<WeatherForecastController> logger, IConfiguration configuration, IOptions<Logging> options) { _logger = logger; _configuration = configuration; logging = options.Value; //logging对象即包含配置文件的信息 }

      单独类读取配置文件信息

      public static class ConfigurationManager
        {
            public readonly static IConfiguration Configuration;
    
            static ConfigurationManager()
            {
                Configuration = new ConfigurationBuilder()
                   .SetBasePath(Directory.GetCurrentDirectory())
                   .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                   .Build();
            }
    
            public static T GetSection<T>(string key) where T : class, new()
            {
                return new ServiceCollection()
                    .AddOptions()
                    .Configure<T>(Configuration.GetSection(key))
                    .BuildServiceProvider()
                    .GetService<IOptions<T>>()
                    .Value;
            }
    
            public static string GetSection(string key)
            {
                return Configuration.GetValue<string>(key);
            }
        }
  • 相关阅读:
    往Matlab中添加工具包
    Deeplearning——Logistics回归
    String类
    机器学习概念性内容整理
    系统的响应与解
    将MathType公式转换为LaTex格式
    【转载】【翻译】Breaking things is easy///机器学习中安全与隐私问题(对抗性攻击)
    在LaTex中插入电路图的方法(插入图片)
    第三章——供机器读取的数据(CSV与JSON)
    第一、二章——Python简介与Python基础
  • 原文地址:https://www.cnblogs.com/kingsmart/p/14985113.html
Copyright © 2011-2022 走看看