当前位置:  开发笔记 > 编程语言 > 正文

asp5 IConfigurationRoot获取json数组

如何解决《asp5IConfigurationRoot获取json数组》经验,为你挑选了3个好方法。

我正在尝试向我的asp 5 mvc 6 web应用程序("rc1-final"库)提供一些额外的配置数据,以便部署在Azure上.此附加数据的一部分由json文件中的数据数组组成.我可以将这个json文件添加到我的配置中,并且在调试时如果我观察配置对象,我可以浏览到提供程序条目,看到它确实联系了我正在寻找的数据数组.

但是,尝试Configuration["extraData"]产生null(因为该属性的"值"为null,即使它显然包含元素数组.

我没有问题访问存储在json配置中的单个值(例如我的数据库连接字符串和其他值.)看起来好像不支持数组格式的json数据?

请参阅以下示例json文件(根据jsonlint进行验证,对我来说似乎很好)...

//extraData.json
{
  "ExtraData": [
    {
      "Id": 1,
      "Description": "The first identifier"
    },
    {
      "Id": 2,
      "Description": "The second identifier"
    },
    {
      "Id": 3,
      "Description": "The third identifier"
    }
  ]
}

...以及以下示例Startup.cs ...

//Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Data.Entity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Linq;
using System.IO;
using Microsoft.AspNet.Authentication.Cookies;
using System.Net;


namespace MyProj {
    public class Startup {
        public static IConfigurationRoot Configuration;

        public Startup(IHostingEnvironment env) {
            var builder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json")
                .AddJsonFile("extraData.json")
                .AddEnvironmentVariables();

            Configuration = builder.Build();
        }

        public void Configure(
            IApplicationBuilder app,
            IHostingEnvironment env,
            MyDbSeed seed,
            ILoggerFactory logging) {

            dynamic xData = JObject.Parse("ExtraData"); //VALUE IS NULL, THROWS EX
            var myList = JsonConvert
                .DeserializeObject>(
                    xData.ExtraData.ToString()
                );
            //Do something important with this list
        }

        public static void Main(string[] args) => WebApplication.Run(args);
    }

    class MyID {
        public int ID { get; set; }
        public string Description { get; set; }
    }
}

我不得不做的事情(这似乎是hackish,特别是考虑到现在可用的方便的配置系统)......

dynamic xData = JObject.Parse(
    File.OpenText(
        env.MapPath(
            @"..\extraData.json"
        )
    ).ReadToEnd()
);
var xList = JsonConvert
    .DeserializeObject>(
        xData.ExtraData.ToString()
    );
//Do something important with the list, eg...
var o = new MyClass(xList);

我也不确定是否支持azure这样做文件io.

我发现这个资源,这表明阵列/支持IEnum,但它仅仅是使用In-MEM提供商...
http://github.com/aspnet/Configuration/blob/dev/test/Microsoft.Extensions.Configuration. Binder.Test/ConfigurationCollectionBindingTests.cs

该资源是来自MS的文档...
http://docs.asp.net/en/latest/fundamentals/configuration.html

自发布以来搜索时出现的一些相关问题(不是1:1与我的问题有关,但可能对其他搜索有用)
如何在startup.cs的Configure方法中使用ConfigurationBinder
从ASP中的config.json中检索部分. NET 5
ASP.NET 5(vNext) - 获取配置设置



1> Shaun Luttin..:

我们现在可以使用GetSectionGetChildren和一些LINQ.

// appsettings.json
{
    "SomeArray": [
        "Foo", 
        "Bar",
        "Baz"
    ]
}

// Startup.cs
var someArray = configuration
    .GetSection("SomeArray")
    .GetChildren()
    .Select(x => x.Value)
    .ToArray();

Configuration库的ArrayTest中还有一个演示.



2> Ricky..:

试试这个:

using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.OptionsModel;
using System.Collections.Generic;

namespace WebApplication1
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            // Set up configuration sources.
            var builder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json")
                .AddJsonFile("extraData.json")
                .AddEnvironmentVariables();

            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; set; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOptions();
            services.Configure>(Configuration.GetSection("ExtraData"));
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            List result = app.ApplicationServices.GetRequiredService>>().Value;

            // you should get what you need. It works in my computer with rc1 update 1
        }

        public static void Main(string[] args) => WebApplication.Run(args);
    }

    public class ExtraData
    {
        public int Id { get; set; }
        public string Description { get; set; }
    }
}

编辑

如果编译器抱怨services.Configure>(Configuration.GetSection("ExtraData")),请添加一个NuGet引用Microsoft.Extensions.Options.ConfigurationExtensions

您也可以将它注入构造函数:

// using Microsoft.Extensions.Options;
public class HomeController : Controller
{
    private List _extraData;
    public HomeController(IOptions> extraData)
    {
        _extraData = extraData.Value;
    }
}



3> Dmitry Pavlo..:

您可以通过以下方式检索设置:

var extraDataSection = configuration.GetSection("ExtraData");
foreach (IConfigurationSection section in extraDataSection.GetChildren())
{
    var id = section.GetValue("Id");
    var description = section.GetValue("Description");
}

推荐阅读
手机用户2502851955
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有