做asp.net mvc时会将一个配置项比如接口地址、MD5盐值写到config文件里,asp.net core mvc没有config文件、有一个appsettings.json,可在这个文件设置。
1、在appsettings.json增加一个ConnectionStrings和配置项
2、读取方法
public string GetConnectionString() { //创建ConfigurationBuilder对象 ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); //给configurationBuilder对象设置appsettings的路径 configurationBuilder.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json"); //获取配置文件中指定的字段值 var configuration = configurationBuilder.Build(); string connentString = configuration.GetConnectionString("Md5Salt"); return connentString; }
来源:https://blog.csdn.net/qq_28315089/article/details/83049048