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

如何在Azure Linux Web App容器中传递字符串数组环境变量

Azure Web App容器中配置.NET Core字符串数组环境变量的正确方法

问题原因分析

你之前尝试的两种方式失效的原因:

  1. 逗号分隔字符串:.NET Core的IConfiguration默认不会自动将单个逗号分隔字符串解析为字符串数组,需手动处理或配置类型转换。
  2. 索引式配置错误:Azure容器环境变量中,数组元素的索引需通过__(双下划线)与数组名分隔,而非直接拼接数字(即Appsettings__EmailRecipients__0而非Appsettings__EmailRecipients0)。

正确配置方法

方法一:Azure Portal中使用标准索引式环境变量

在Azure Web App的「配置」→「应用程序设置」中添加以下环境变量:

  • 名称:Appsettings__EmailRecipients__0,值:devlopers@gmail.com
  • 名称:Appsettings__EmailRecipients__1,值:admin.user@gmail.com

保存配置后重启Web App,后端可通过以下代码获取数组:

var emailRecipients = Configuration.GetSection("Appsettings:EmailRecipients").Get<string[]>();

方法二:逗号分隔字符串+后端手动解析

  1. 在Azure中配置环境变量:

    Appsettings__EmailRecipients: "devlopers@gmail.com,admin.user@gmail.com"
    

    (若逗号前后有空格,解析时需处理空格)

  2. 后端获取并解析:

    var recipientsStr = Configuration["Appsettings:EmailRecipients"];
    var emailRecipients = recipientsStr?.Split(',', StringSplitOptions.RemoveEmptyEntries)
                                        .Select(s => s.Trim())
                                        .ToArray();
    
  3. 若使用强类型配置绑定,可通过类型转换器自动解析:

    using System.ComponentModel;
    
    public class AppSettings
    {
        [TypeConverter(typeof(CommaSeparatedStringConverter))]
        public string[] EmailRecipients { get; set; }
    }
    

    在Program.cs中绑定配置:

    builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("Appsettings"));
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 23:42:41