哪种方式从 local.settings.json .NET6 获取值?

发布于 2025-01-14 20:49:24 字数 1056 浏览 2 评论 0原文

有人可以解释一下哪种方式(如果有的话)是从 local.settings.json、C# .NET6 中获取 Startup 类中的值的首选方式。 startup.cs 用于一个函数。

            client.BaseAddress = new Uri(configuration["BaseEndpoints:RestIg"]);
            client.BaseAddress = new Uri(Environment.GetEnvironmentVariable("BaseEndpoints:RestIg"));
            client BaseAddress = new Uri(configuration.GetSection("BaseEndpoints:RestIg").Value);

在我的 local.settings.json 中,我可以将 BaseEndpoints:RestIg 的值设置在 value{} 内部或外部,但不确定我想将其放置在哪里?

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"TimerInterval": "0 0 * * * *"
},
"BaseEndpoints": {
"RestIg": "https://myurl.com....etc.etc.etc" 
 }
}

根据此Microsoft 推荐此方法,因为它既可以在本地使用 local.settings.json,也可以在 Azure 中使用。 即
Environment.GetEnvironmentVariable("BaseEndpoints:RestIg");

Could someone please explain which way, if any of these, is the preferred way of fetching values in Startup class from local.settings.json, C# .NET6. The startup.cs is for a function.

            client.BaseAddress = new Uri(configuration["BaseEndpoints:RestIg"]);
            client.BaseAddress = new Uri(Environment.GetEnvironmentVariable("BaseEndpoints:RestIg"));
            client BaseAddress = new Uri(configuration.GetSection("BaseEndpoints:RestIg").Value);

In my local.settings.json I can have the value of BaseEndpoints:RestIg either inside or outside the values{}, not sure where I would want to place it?

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"TimerInterval": "0 0 * * * *"
},
"BaseEndpoints": {
"RestIg": "https://myurl.com....etc.etc.etc" 
 }
}

According to this dude Microsoft recommends this method because it works both locally with local.settings.json and in Azure.
i.e.
Environment.GetEnvironmentVariable("BaseEndpoints:RestIg");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文