c# - 从 Azure 应用程序配置存储获取值,其中内容类型在 C# 中设置为 application/json

标签 c# azure asp.net-core azure-functions azure-app-configuration

我正在尝试从 Azure 应用程序配置存储中获取值。 我创建了一个 key - Credentials 及其值

{
      "Name": "MyNewCsv",
      "Extension": "csv",
      "Delimeter": "|"
}

我已将其内容类型设置为 application/json

我正在尝试从 C# 代码中读取此键值,如下所示:

private readonly IConfiguration _configuration;
        public Helper(IConfiguration configuration)
        {
            _configuration = configuration;
        }

public FileConfiguration GetFileConfiguration(string azureKeyName)
        {
            string message = _configuration[azureKeyName];
            var fileConfiguration = JsonConvert.DeserializeObject<FileConfiguration>(message);
            return fileConfiguration;
        }

我将 azureKeyName 作为 Credentials 传递,但当我调试代码时,message 为 null,即使 key 是在 Azure 上创建的。 当我从应用程序配置存储中删除 application/json 内容类型时,此代码可以正常工作。

为什么我在设置内容类型时遇到麻烦? 请帮助我当内容类型设置为 application/json 时如何读取键值?

请帮忙。

enter image description here

谢谢。

最佳答案

这是因为Credentials 在应用程序配置中保存为 JSON 对象。因此,将其读取为字符串是不正确的。您必须创建一个如下所示的类:

public class Credentials{
   public string Name{get;set;}
   public string Extension{get;set;}
   public string Delimeter{get;set;}
}

然后,像下面这样阅读:

Credentials message = _configuration.GetSection(azureKeyName).Value;

关于c# - 从 Azure 应用程序配置存储获取值,其中内容类型在 C# 中设置为 application/json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68933866/

相关文章:

python - Azure Functions Blob 部署

c# - ByteArray 到 IFormFile

c# - 直接从数据库返回 IEnumerable 或之前使用 ToListAsync

c# - 如何根据消息属性筛选 Azure 服务总线队列消息?

c# - Kinect "sleep"命令

azure - 验证 Azure APIM 策略中的 JWT

c# - 如何实现Azure表分页

c# - .net core 中的默认最大请求长度是多少

c# - 有没有办法在 Visual Studio 扩展窗口中托管 C# 代码编辑器

c# - 在 Oxyplot 的线系列数据点中添加字符串值