在Azure Web应用程序设置中使用数组

问题描述 投票:21回答:2

在我的ASP.NET 5(RC1)代码中,我有一个看起来像这样的appsetting.json:

{
    "SomeSettings": {
        "PropA": "ValueA",
        "PropB": [
            "ValueB1",
            "ValueB2"
        ]
    }
}

这些值在我的开发机器(即本地主机)上运行代码时使用。如果要覆盖wep应用程序在Azure应用程序设置中的“ SomeSettings”,如何指定“ PropB”数组?

我要在其中存储信息的SomeSettings.cs类如下:

public class SomeSettings
{
    public string PropA { get; set; }
    public List<string> PropB { get; set; }
}

问题是“ PropB”-如何在Azure中将数组或列表指定为字符串-甚至可能吗?

在我的Startup类的构造函数中,我有:

var builder = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .AddEnvironmentVariables();

并且在我的Startup类的Configure方法中:

var someSettings = configuration.GetSection("SomeSettings").Get<SomeSettings>();
c# azure asp.net-core azure-web-sites
2个回答
55
投票

像这样在“应用设置”下添加设置将达到目的...请注意下面的“:0”和“:1”

格式:键->值

SomeSettings:PropA -> AzureValueA
SomeSettings:PropB:0 -> AzureValueB1
SomeSettings:PropB:1 -> AzureValueB2

如果您不是在Windows上运行,请用双下划线:替换冒号__,以使您的应用查看设置。所以而不是例如SomeSettings:PropA,您将使用SomeSettings__PropA


-6
投票

简单方法是将JSON作为字符串存储在AppSetting中,然后自行反序列化

var serializer = new JavaScriptSerializer();
var settings = serializer.Deserialize<SomeSettings>(configuration.GetSection("SomeSettings"));

否则,您将不得不创建自己的客户配置。 https://msdn.microsoft.com/en-us/library/2tw134k3.aspx

© www.soinside.com 2019 - 2024. All rights reserved.