![](/img/trans.png)
[英]Using Application settings of Azure App Service resulting into an HTTP 500 error
[英]Override an array in azure app service application settings
在我的 .NET Core 應用程序中,我向 appsettings.json 添加了一個數組,如下所示:
{
"SettingsA": {
"PropA": [
"ChildObjectA": {
...
},
"ChildObjectB": {
...
}
]
}
}
如果我想從我的 azure 應用程序服務中的應用程序設置中覆蓋該值,以便它有空數組:
{
"SettingsA": {
"PropA": []
}
}
有沒有辦法做到這一點?
我試着把
SettingsA:PropsA -> []
在應用程序設置中,但似乎沒有覆蓋 appsettings.json 的值
這里的答案https://www.ryansouthgate.com/2016/03/23/iconfiguration-in-netcore/是您可以覆蓋數組中的元素或添加其他元素,但他說您不能覆蓋整個數組,這看起來很奇怪。
覆蓋語法使用從零開始的訪問,例如 SettingsA:PropA:0:Something,我已經在 App Services 上嘗試過,可以確認它有效。
為了補充這里已經給出的所有優秀答案,以下是我們在現實生活場景中的做法。
我們需要在我們的應用程序設置中配置一組受支持的語言。 下面是它在我們的 .NET Core 項目中的 appSettings.json 中的樣子:
{
...
"SupportedLanguages": [
{
"Code": "en-AU",
"Name": "English (Australia)"
},
{
"Code": "en-GB",
"Name": "English (United Kingdom)"
},
{
"Code": "en-US",
"Name": "English (United States)"
}
]
}
這就是它最終在我們的 Azure 應用服務中的樣子:
這有點繁瑣,特別是如果您有更大或更復雜的層次結構,但我認為目前沒有其他方法。
您可以使用AddEnvironmentVariables
屬性將azure 上的AddEnvironmentVariables
覆蓋到本地設置。
注意:這里的值為空。
要覆蓋 App Settings 部分中的嵌套鍵,我們可以使用完整路徑SettingsA:PropA
作為名稱或使用雙下划線SettingsA__PropA
來定義變量。 你可以參考這篇文章。
在本地,您可以配置如下: 在 Startup.cs 中:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
configuration = builder.Build();
}
public IConfiguration configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddOptions();
services.Configure<SettingsOptions>(configuration.GetSection("SettingsA"));
}
在 appsettings.json 中:
{"SettingsA": {
"PropA": ["a","b"]
}
}
在家庭控制器中:
private readonly IOptions<SettingsOptions> options;
public HomeController(IOptions<SettingsOptions> options)
{
this.options = options;
}
public IActionResult Index()
{
var value = options.Value;
ViewBag.Index = value.PropA+"success";
return View();
}
在設置選項中:
public class SettingsOptions
{
public string SettingsA { get; set; }
public string PropA { get; set; }
}
將項目發布到 azure 后,它將覆蓋 PropA 值。 有關如何從 asp.net core 讀取 appsetting 的更多詳細信息,請關注此案例。
據我所知, 應用程序設置中的設置在運行時注入到您的appsettings.json
中,覆蓋現有設置。 它不會更改 appsettings.json 文件,因為 Azure GUI(連接字符串、應用程序設置)在內部使用環境變量。
這里有一個類似的帖子供您參考。 您可以在發布期間通過 VSTS 覆蓋設置。 這里是Colin 的 ALM Corner Build & Release Tools和教程的鏈接。 更多詳情,你可以參考上面帖子中psycho的回復。
如果需要在 VSTS 發布活動期間(在發布到 Azure 之前)覆蓋 appsettings.json 的值,可以使用 Colin 的 ALM Corner Build & Release Tools。
這是微軟團隊非常奇怪的行為。 看起來他們不知道現實是什么樣子。
我最終得到了一個用逗號分隔的值而不是數組的字符串。
為此,在 Azure 上使用多平台語法使用 __
這種方式適用於 Windows 和 Linux 部署的應用服務:
SettingsA__PropA__0__Something
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.