75

In my ASP.NET 5 (RC1) code I have an appsetting.json that looks something like this:

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

These value are used when a run the code on my dev machine (ie. localhost). If I want to overwrite the "SomeSettings" in Azure's Application settings for the wep app, how would I specify the "PropB" array?

The SomeSettings.cs class that I want to store the information in looks like this:

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

The problem is "PropB" - how to I specify an array or List as a string in Azure - is this even possible?

In my Startup class's constructor I have:

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

And in my Startup class's Configure methods I have:

var someSettings = configuration.GetSection("SomeSettings").Get<SomeSettings>();

4 Answers 4

163

Adding the settings under "App settings" like this will do the trick... Notice the ":0" and ":1" below

Format: Key -> Value

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

If you aren't running on Windows, replace the colon : with double underscore __ to get your app to see the settings. So instead of e.g. SomeSettings:PropB:1, you'd use SomeSettings__PropB__1.

Sign up to request clarification or add additional context in comments.

9 Comments

As of today it works great. Just want to confirm approach is still here. We use VSTS to deploy WebApp arguments via PS scripts, now we can push arrays. Thanks.
Do you know how you would represent SomeSettings__PropB as an empty array?
This also works in Azure Functions' local.settings.json.
For an unnamed array of object use sectionName:0:PropA sectionName:0:PropB sectionName:1:PropA sectionName:1:PropB etc. For Linux services replace : with double underscore __
This still works in 2023 on app services, for APIs or azure functions.
|
2

In case the array value is an object (see that WriteTo value below) you can copy the entire WriteTo value, update the values if you need to and create an application setting for it as follows;

  "Serilog": {
    "WriteTo": [
      {
        "Name": "ApplicationInsights",
        "Args": {
          "restrictedToMinimumLevel": "Information",
          "telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights",
          "instrumentationKey": "YOUR-KEY"
        }
      },
      {
        "Name": "UmbracoFile",
        "Args": {
          "RestrictedToMinimumLevel": "Error"
        }
      },
      {
        "Name": "Async",
        "Args": {
          "configure": [
            {
              "Name": "Console"
            }
          ]
        }
      }

    ]
  }

enter image description here

2 Comments

I can't seem to get this to work. I try to use if to feature management filters that has a crazy amount for properties and arrays. Is there anything special that needs to happen for this to work?
No, nothing special. It is exactly how I described it in my answer. Hope this helps.
0

Following the documentation the value should be like this:

['entry1', 'entry2', 'entry3']

https://learn.microsoft.com/en-us/azure/app-service/configure-common?tabs=portal#configure-arrays-in-app-settings

3 Comments

It's in the official documentation, but this doesn't work for me. The array that I have in my appsettings.json is not being overridden.
Since the linked doc doesn't seem to show anything about arrays anymore, here's something else that clarifies the obscure convention: learn.microsoft.com/en-us/aspnet/core/fundamentals/… The double-underscore notation may or may not apply (obscure in its own right), but the key is that "the array index should be treated as an additional element name in this path".
this works fine for me, not sure if the array is making any difference ( options object is defined as array not list ) but I do have the configurations as an array and I use IOptions and it's working
-10

Simple approach is store the JSON as string in AppSetting, and de-serialize by yourself

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

or you will have to create your own customer configuration i believe. https://msdn.microsoft.com/en-us/library/2tw134k3.aspx

1 Comment

The question is about "Azure web app settings", your answer is irrelevant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.