【问题标题】:How can I access appsettings.json via PowerShell in a deployed Azure Web App?如何在已部署的 Azure Web 应用程序中通过 PowerShell 访问 appsettings.json?
【发布时间】:2016-11-02 20:50:18
【问题描述】:

我需要能够在已部署的 Azure Web 应用中修改 appsettings.json 中的设置。

我可以通过以下命令通过 PowerShell 访问 AppConfig 和 Connectionstrings:

$webApp = Get-AzureRmWebApp -ResourceGroupName $resourceGroupName -Name $websiteName
$webApp.SiteConfig.AppSettings
$webApp.SiteConfig.ConnectionStrings

但是,是否可以通过 PowerShell 访问 appsettings.json?

【问题讨论】:

  • 你可以,但是你会读/写一个文件,这是你的部署的一部分。您确定要这样做吗,不建议采用最佳做法?我不完全确定您要做什么,但我想不出有什么好的理由让脚本在部署后更新静态配置文件。如果您能告诉我们用例是什么,或许我们可以建议一种更好的方法来实现您的目标。

标签: .net azure asp.net-core azure-powershell


【解决方案1】:

据我所知,您可以在 Azure Web 应用中添加应用设置以覆盖 appsettings.json 文件中的配置值。我假设您的 appsettings.json 文件如下所示:

{
  "OAuth": {
    "ApiKey": "ApiKey",
    "ApiSecret":"ApiSecret"
  }
}

然后,您可以按照以下脚本修改您的设置:

$resourceGroupName="<your-resource-group-name>"
$websitename="<your-web-app-name>"
$webApp = Get-AzureRmWebApp -ResourceGroupName $resourceGroupName -Name $websitename
$appsettingList=$webApp.SiteConfig.AppSettings

#current appsettings
$appsettingList

$appsettings = @{}
ForEach ($k in $appsettingList) {
    $appsettings[$k.Name] = $k.Value
}

#modify the settings
$appsettings['OAuth:ApiKey'] = "<new-api-key>"
$appsettings['OAuth:ApiSecret'] = "<new-api-secret>"

#save appsettings
set-AzureRmWebApp -resourcegroupname $resourceGroupName -name $websitename -appsettings $appsettings

【讨论】:

  • 感谢您的回复。不幸的是,SiteConfig.AppSettings 似乎只返回通过 Azure 门户定义的应用程序设置。在网站发布后,我在本地开发时对 appSettings.json 所做的任何自定义添加都无法通过上述方法获得。任何想法如何访问它们?
猜你喜欢
  • 2022-01-10
  • 2021-01-27
  • 1970-01-01
  • 2017-11-05
  • 1970-01-01
  • 2014-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多