IConfiguration服务是net core 默认依懒注入的一个对象 直接注入(推荐使用)

  新建控制器 然后构造函数直接注入

 [Route("api/[controller]")]
public class ValuesController : Controller
{
private IConfiguration _configuration { get; } public ValuesController(IConfiguration configuration)
{
_configuration = configuration;
}
// GET: api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { _configuration["appSettings:SvcUrl"] };
}
}

创建一个ConfigurationBuilder 来加载配置文件

  var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
var configuration = builder.Build(); string SvcUrl = configuration["appSettings:SvcUrl"];
04-29 00:03