.Net Core configuration read appsettings.json

In the .net core is not * .config file configuration files are * .json

1, in the following line of code in project.json

"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",

Then modify appsettings.json add Redis configuration

Copy the code
  // user profile 
  "the AppSettings": { 
    // the Redis cache 
    "RedisCaching": { 
      "Enabled": to true, 
      "the ConnectionString": "10.18.200.177:6379" 
    } 
  }
Copy the code

2, a new object corresponding to AppSettings profile information

Copy the code
    /// <summary>
    /// 配置文件
    /// </summary>
    public class AppSettings
    {
        public RedisCaching RedisCaching { get; set; }
    }
    /// <summary>
    /// Redis
    /// </summary>
    public class RedisCaching {
        /// <summary>
        /// 是否启用
        /// </summary>
        public bool Enabled { get; set; }
        /// <summary>
        /// 链接信息
        /// </summary>
        public string ConnectionString { get; set; }
    }
Copy the code

3, in the configuration where Startup.cs first with addOptions () initializes injection IOptions <T>, and Configure <AppSettings> AppSettings instance is automatically initialized and mapped in the configuration appSettings

services.AddOptions();
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));

 4, using

Guess you like

Origin www.cnblogs.com/Jeely/p/10959113.html