.NET Core use Configuration to bind to Options with Array

前端 未结 3 1589
傲寒
傲寒 2021-02-12 15:50

Using the .NET Core Microsoft.Extensions.Configuration is it possible to bind to a Configuration to an object that contains an array?

3条回答
  •  误落风尘
    2021-02-12 16:16

    You can configure ExampleOptionwith code in ConfigureServices method:

     public void ConfigureServices(IServiceCollection services)
     {
          services.Configure(myOptions =>
          {
              myOptions.Array = new int[] { 1, 2, 3 };
          });
     }
    

    or if you want to use json configuration file

    appsettings.json:

    {
      "ExampleOption": {
         "Array": [1,2,3]
      }
    }
    

    ConfigureServices:

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure(Configuration.GetSection("ExampleOption"));
    }
    

提交回复
热议问题