![](/img/trans.png)
[英]How to read section values from appsettings.json in .NET Core
[英]Read values from appsettings.json before configuration.build
是否可以在 configuration.build 之前從 appsettings.json 讀取值?
我需要獲取端點、clientId、secret 和租戶以將配置與 azure 應用程序配置綁定
Configuration = builder.AddAzureAppConfiguration(options =>
{
//here i need to get some values from appsettings.json
}).Build()
我不確定這一點,因為我通常使用 WebHostBuilder,但我認為它應該是這樣的:
var settings = builder.Build();
Configuration = builder.AddAzureAppConfiguration(options =>
{
options.Connect(settings["ConnectionStrings:AppConfig"])
}).Build();
編輯:注意我假設您已經將 appsettings.json 添加到構建器中。 如果沒有,您需要添加
var env = [something like]hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
您可以像這樣使用ConfigurationBuilder
:
var builder = new ConfigurationBuilder()
.SetBasePath("yourJsonPath")
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
var config = builder.Build();
然后你可以檢索你需要的值:
config.GetSection("AppConfig:Endpoint").Value
要不就
config.GetSection("ClientId").Value
取決於您的 JSON 的構建方式。
這是不可能的。
以下是 Azure 應用程序配置 GitHub 示例中與您的場景類似的兩個示例:
該示例顯示,構建ConfigurationBuilder
是為了獲取中間IConfiguration
,然后使用該中間 IConfiguration 提供連接字符串,以將 Azure 應用程序配置添加到構建器。
在 ASP.NET Core web 應用示例中,相同的模式與框架提供的IConfigurationBuilder
一起使用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.