You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过.NET 7 C#控制器创建/更新Azure App Config配置项

可以用C#实现Azure App Config的键值对创建/更新操作

完全可以通过.NET 7的C#代码实现这个需求,核心是使用官方的Azure.Data.AppConfiguration SDK来操作Azure App Config。下面是具体实现步骤:

1. 安装NuGet包

首先在项目中安装Azure App Configuration的客户端SDK:

Install-Package Azure.Data.AppConfiguration

或者通过.NET CLI:

dotnet add package Azure.Data.AppConfiguration

2. 配置连接信息

在appsettings.json中添加Azure App Config的连接字符串(可从Azure门户获取):

{
  "AzureAppConfigConnectionString": "你的App Config连接字符串"
}

3. 定义接收请求的模型

创建一个用于接收JSON键值对的模型类:

public class ConfigKeyValue
{
    public string Key { get; set; }
    public string Value { get; set; }
    // 可选:如需标签或其他属性,可添加对应字段
    public string Label { get; set; } = string.Empty;
}

4. 编写API控制器

创建API控制器,接收POST请求并处理键值对的创建/更新逻辑:

using Azure.Data.AppConfiguration;
using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("api/[controller]")]
public class AppConfigController : ControllerBase
{
    private readonly ConfigurationClient _configurationClient;

    // 通过依赖注入获取ConfigurationClient实例
    public AppConfigController(IConfiguration configuration)
    {
        var connectionString = configuration["AzureAppConfigConnectionString"];
        _configurationClient = new ConfigurationClient(connectionString);
    }

    [HttpPost]
    public async Task<IActionResult> UpsertConfig([FromBody] ConfigKeyValue config)
    {
        if (string.IsNullOrWhiteSpace(config.Key))
        {
            return BadRequest("键不能为空");
        }

        // 创建或更新配置项
        var setting = new ConfigurationSetting(config.Key, config.Value)
        {
            Label = config.Label
        };

        await _configurationClient.SetConfigurationSettingAsync(setting);

        return Ok("配置项已成功保存");
    }

    // 可选:批量处理键值对的接口
    [HttpPost("batch")]
    public async Task<IActionResult> UpsertConfigBatch([FromBody] List<ConfigKeyValue> configs)
    {
        if (configs == null || !configs.Any())
        {
            return BadRequest("没有提供有效的配置项");
        }

        var tasks = configs.Where(c => !string.IsNullOrWhiteSpace(c.Key))
                           .Select(c => 
                           {
                               var setting = new ConfigurationSetting(c.Key, c.Value)
                               {
                                   Label = c.Label
                               };
                               return _configurationClient.SetConfigurationSettingAsync(setting);
                           });

        await Task.WhenAll(tasks);

        return Ok("批量配置项已成功保存");
    }
}

5. 权限说明

确保你的应用拥有Azure App Config的**数据参与者(Data Contributor)或数据所有者(Data Owner)**权限,否则会出现权限不足的错误。若使用托管身份而非连接字符串,需额外配置Azure AD身份验证逻辑。

关键说明

  • SetConfigurationSettingAsync方法会自动处理"存在则更新,不存在则创建"的逻辑,无需额外判断配置项是否存在。
  • 可根据需求添加标签、内容类型等额外属性,ConfigurationSetting类提供了对应的字段。

内容的提问来源于stack exchange,提问作者Serhat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 07:47:13