0

I am trying to create (update if exists) Azure App Config values through .NET 7 C# controller.

The goal is basically send a key-value pair as json through my controller and let my backend process save that key-value pair to Azure App Config.

So the question is: can I send a key-value pair to Azure App Config through my C# code?

PS: I know it is possible through Azure CLI and Importing through portal :)

5
  • Yes,It can be done from the Controller Action method. Commented Feb 26, 2024 at 13:17
  • Iam able to do it using the SetConfigurationSetting. Refer this MSDoc once.Image Commented Feb 26, 2024 at 13:18
  • a key-value pair as json through my controller, can you share a sample json once. Commented Feb 26, 2024 at 13:23
  • I got the sol how to create appconfig from code.If you send the json format and from where you want to send.I will post an answer in detail. Commented Feb 26, 2024 at 13:40
  • it can be from anywhere, controller or a service. I just want to set a key-value pair. both are a string. like so {"key":"value"} . So my controller basically accepts a Dictionary<string, string> Commented Feb 26, 2024 at 14:59

1 Answer 1

1

Can I send a key-value pair to Azure App Config through my c# code?

Yes, you can send a key-value from Controller Action Method and create a key-value pair in Azure App Configuration.

  • As mentioned in the MSDoc, I have used SetConfigurationSetting to create a Key-Value pair.

My appsettings.json file:

"AppConfig": "Endpoint=https://harshuappconfig.azconfig.io;Id=RoKQ;Secret=****************"

My Program.cs file:

var appConfig = builder.Configuration["AppConfig"];
builder.Services.AddAzureAppConfiguration();

My Controller ActionMethod:

  private readonly IConfiguration config;
  private readonly ConfigurationClient configClient;

public HomeController(IConfiguration myconfig, ILogger<HomeController> logger)
 {
     _logger = logger;
     config = myconfig;
     var connectionString = config["AppConfig"];

     configClient = new ConfigurationClient(connectionString);
 }
 public IActionResult Index()
 {
     var KVPair = JObject.Parse(@"{""Name"":""Harshitha"",""SurName"":""Veeramalla""}");
     foreach (var kv in KVPair)
     {

         Console.WriteLine($"Key '{kv.Key}' updated value '{kv.Value}' in Azure App Config.");
         var setting = new ConfigurationSetting(kv.Key, kv.Value.ToString());
         configClient.SetConfigurationSetting(setting);
     }
     return View();
 }
  • Here I am sending the JSON object in a string.

Output: enter image description here

enter image description here

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

7 Comments

For the same request it updates the value to the existing key.
I tried it from my end.For new request a new key-value will be created.For an existing key it updates the value.
Thank you Harshitha. I really appreciate your time and effort in helping me out.
Have you checked it from your end?
@NaveenSharma yes. I was going to do that when i got an answer to my question as a comment. Thank you for pointing it out again :)
|

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.