如何从appsetting.json读取json数据

时间:2020-04-29 19:23:30

标签: c# asp.net asp.net-core .net-core blazor

我想使用blazor C#从appsetting.json文件中读取一些数据。这是代码

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "Test1": "Server=KHITGFDB;Database=TESTDB;Trusted_Connection=True;",
    "AuditDatabase": "Server=KHITGGF;Database=TISA;Trusted_Connection=True;",
    "EpriseDatabase": "Server=BHUIL,50397;Database=DUON;User ID=yHISNeBHID;Password=HGTUKHJSAC;"
  },
  "EmailRecipients": "a@test.com;b@test.com:c@test.com"
}

我想从appsetting.json到text.razor页面读取EmailRecipients。这是下面的代码。

Test.razor

 public void saveShareFeedback()
    {
        if (feedback.Rating == 0) return;

        feedback.UserId = "Tester";
        feedback.EmailRecipients = "a@test.com;b@test.com:c@test.com";


        var ack = _db.SaveFeedbackData(feedback);
        if (ack == true)
        {
            isSharedFeedback = true;
            msg = "Thanks for sharing your feedback...";
            feedback = new Feedback();
        }

    }

1 个答案:

答案 0 :(得分:2)

我希望这是服务器端Blazor。注入IConfiguration。您可以使用此方法在appsettings.json中获取任何存储的值(如果未从命令行参数覆盖该值)。

string emailRecipients = _configuration["EmailRecipients"];