【问题标题】:Can you read from multiple .JSON files in one ASP.NET CORE project?您可以在一个 ASP.NET CORE 项目中读取多个 .JSON 文件吗?
【发布时间】:2021-05-24 13:09:42
【问题描述】:

我正在解决一个问题,该问题需要我根据网页的 URL 从两个不同的 .Json 文件中读取,我似乎找不到除 Iconfiguration 之外的任何其他读取方式。有什么方法可以确定配置的路径吗?像 (/site/webURL/appsetting.json/) 然后选择我想要获取的值的名称 (_configuration["privacy:GDPR"]) 我从普通的 appsettings 中读取没有问题。我只是想知道我是如何从具有特定路径的 .json 中读取信息的

public static string Getjsonvalue(string hostName = "")
        {
            string hostNam = string.IsNullOrEmpty(hostName) ? _options.Value.Paths.Defaultsite : hostName.Replace('-', '_').Replace('.', '_');

            var rootPath = ("/sites/" + GetSite(hostName) + "/appsetting.json").Replace('\\', '/');
            var Value =  find configuration["privacy:GDPR"] in rootPath// where i need help
            return Value;
        }

【问题讨论】:

  • 这个问题已经回答Question
  • @Majid 没有回答我的问题。通过给我的应用程序一个路径,我希望能够同时使用 3 或 4 个配置文件。

标签: asp.net-core configuration


【解决方案1】:

通过路径读取文件的一种方法可以是这样的:

string contentRootPath = _hostingEnvironment.ContentRootPath;
var JsonFile = System.IO.File.ReadAllText( contentRootPath + "/yourJsonFile.json");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    相关资源
    最近更新 更多