【问题标题】:Reading a List of Json Objects from config file and return it in the controller always return null从配置文件中读取 Json 对象列表并在控制器中返回它总是返回 null
【发布时间】:2021-01-18 09:02:58
【问题描述】:

我有一个这样的 GameSettingOptions 类:

public class GameSettingOptions
    {
        public int gameId { get; set; }
        public string iconSize { get; set; }
        public int sortOrder { get; set; }
        public string[] chips { get; set; }
    }

我正在尝试从这样设置的服务中检索它:

 public Startup(IConfiguration configuration, IHostingEnvironment environment)
        {
            
            var builder = new ConfigurationBuilder()
                .SetBasePath(environment.ContentRootPath)
                .AddJsonFile("GameSettingOptions.json", optional: true, reloadOnChange: true);
            Configuration = builder.Build();
        }
public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddCors(c =>
            {
                c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin());
            });
            services.Configure<GameSettingOptions>(Configuration.GetSection("GameSettings"));
        }

这是我的 json 对象,其中游戏设置是一个数组:

{
  "GameSettings": [
    {
      "gameId": 1,
      "iconSize": "big",
      "sortOrder": 6
    },
    {
      "gameId": 2,
      "iconSize": "small",
      "sortOrder": 4
    }
  ]
}

这就是我尝试使用此控制器和依赖注入来检索它的方式

public class GameSettingsControllers : ControllerBase
    {
        private readonly GameSettingOptions Options;
        public GameSettingsControllers(IOptionsSnapshot<GameSettingOptions> options)
        {
            Options = options.Value;

        }

        public object GetGameSettings()
        {
            
                return new JsonResult($"gameId: {Options.gameId}, " + $"iconSize: {Options.iconSize}, "
                    + $"sortOrder: {Options.sortOrder}, " + $"chips: {Options.chips} ");
          
        }
    }

我尝试过的:

  1. 将服务和使用服务的属性都变成一个列表,这将返回 null
services.Configure<List<GameSettingOptions>>(Configuration.GetSection("GameSettings"));
IOptions<List<GameSettingOptions>>
  1. 创建另一个具有 GameSettingOptions 属性集合的类,然后绑定并注入新类,这也返回 null
public class GameSettings
{
  public GameSettingsOptions[] AllGameSettings {get; set;}
}

public class GameSettingOptions
    {
        public int gameId { get; set; }
        public string iconSize { get; set; }
        public int sortOrder { get; set; }
        public string[] chips { get; set; }
    }
  1. 对于控制器,我尝试执行 IOptions 值的 foreach,但它说 GameSetingsOptions 敌人不包含公共实例或“GetEnumerator”的定义

【问题讨论】:

标签: c# json configuration app-config options


【解决方案1】:

请定义GameSettingsOtions,如下所示

public class GameSettingOptions: List<GameSetting>
{
}

public class GameSetting
{
    public int gameId { get; set; }
    public string iconSize { get; set; }
    public int sortOrder { get; set; }
    public string[] chips { get; set; }
}

现在配置它,使用下面的代码ConfigureServices方法:

services.Configure<GameSettingOptions>(options => Configuration.GetSection("GameSettings").Bind(options));

通过上面你可以看到定义的选项被注入。这是我调试的截图:

这是我的 appsettings.json 文件供参考:

{
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft": "Warning",
            "Microsoft.Hosting.Lifetime": "Information"
        }
    },
    "AllowedHosts": "*",
    "MySettings": {
        "SettingValue": 2
    },
    "GameSettings": [
        {
            "gameId": 1,
            "iconSize": "big",
            "sortOrder": 6
        },
        {
            "gameId": 2,
            "iconSize": "small",
            "sortOrder": 4
        },
        {
            "gameId": 3,
            "iconSize": "medium",
            "sortOrder": 2
        },
        {
            "gameId": 4,
            "iconSize": "small",
            "sortOrder": 5
        },
        {
            "gameId": 5,
            "iconSize": "small",
            "sortOrder": 8,
            "chips": []
        },
        {
            "gameId": 6,
            "iconSize": "small",
            "sortOrder": 7
        },
        {
            "gameId": 7,
            "iconSize": "big",
            "sortOrder": 1,
            "chips": []
        }
    ]
}

【讨论】:

  • 我应该在 foreach 循环中返回它吗?
  • 为什么是 foreach。 GameSettingOptions 代表整个数组。现在您想要特定的游戏 ID,然后使用 Linq 过滤记录。 var game = gameSettingOptions.FirstOrDefault(g =&gt; g.gameId == 1 - 这给了 gameId 1 个对象
  • 谢谢你,我让它工作了。顺便问一下,你如何获取我的 json 配置文件的信息?
  • 来自之前删除的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-29
  • 2017-09-03
  • 1970-01-01
  • 2015-09-16
  • 1970-01-01
相关资源
最近更新 更多