如何在.NET Core中的IConfiguration中获取自定义环境变量 公共静态IWebHostBuilder CreateWebHostBuilder(字符串[]args)=> WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration(a=>{ a、 AddEnvironmentVariables(); }) .UseStartup();
在我这样调用的API内部,它不支持自定义变量,如何获取自定义变量 我已经在系统中创建了一个自定义变量如何在.NET Core中的IConfiguration中获取自定义环境变量 公共静态IWebHostBuilder CreateWebHostBuilder(字符串[]args)=> WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration(a=>{ a、 AddEnvironmentVariables(); }) .UseStartup();,.net,.net-core,.net,.net Core,在我这样调用的API内部,它不支持自定义变量,如何获取自定义变量 我已经在系统中创建了一个自定义变量 public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration(a => { a.AddEnvironmentVariables(); })
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(a => {
a.AddEnvironmentVariables();
})
.UseStartup<Startup>();
public ActionResult Get(){
var t=configuration.GetSection(“yash2”).值;
返回新字符串[]{“value1”,“value2”};
}
您的appsettings.json
public ActionResult<IEnumerable<string>> Get() {
var t = configuration.GetSection("yash2").Value;
return new string[] { "value1", "value2" };
}
在你的c代码中,如果你想直接访问
{
"yash1": "test1",
"yash2": {
"BaseUrl": "test.com/api",
"Suffix": "test"
},
"yash3": "test3",
}
var yash2BaseUrlVal=configuration.GetSection(“yash2”).GetValue(“BaseUrl”);
var yash1=配置[“yash1”];
否则,创建配置类并访问它。
我相信他是想引入系统环境变量,而不是使用appsettings.json文件。要从环境变量中引入节,环境变量需要类似于“
yash2\uuu BaseUrl
”并设置为“value1
”configuration.GetSection(“yash2”)
将返回一个配置节,其中包含前缀为yash2\uuuu
的所有环境变量。
var yash2BaseUrlVal = configuration.GetSection("yash2").GetValue<string>("BaseUrl");
var yash1 = configuration["yash1"];