结合了好几个哥们的写法,最终我使用了这个版本,现在把我这个版本的写出来,如果该版本和您的版本类似或者一样,那可能是借鉴您的
using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.Json; using System; using System.Collections.Generic; using System.Text; namespace Common { public class AppSettings { public static IConfiguration Configuration { get; set; } public static void Initial(IConfiguration configuration) { Configuration = configuration; } /// <summary> /// 封装要操作的字符 /// </summary> /// <param name="sections"></param> /// <returns></returns> public static string app(params string[] sections) { try { var val = string.Empty; for (int i = 0; i < sections.Length; i++) { val += sections[i] + ":"; } return Configuration[val.TrimEnd(':')]; } catch (Exception) { return ""; } } } }
然后我们需要在strartup里面为这个Configuration赋值
public Startup(IConfiguration configuration, IWebHostEnvironment env) { AppSettings.Initial(configuration); }
在上面这个里面我看见有个老哥的写法是下面这种,但是经过我的测试,直接按照我的这种写法操作也是可以的
该图借鉴于:https://www.cnblogs.com/wangpengzong/p/12700974.html