【问题标题】:How to add and access key value pairs from appsettings.config file in visual studio code?如何在 Visual Studio 代码中从 appsettings.config 文件添加和访问键值对?
【发布时间】:2019-11-04 05:02:41
【问题描述】:

我在控制台应用程序中使用 .net 核心和 sdk 在 Visual Studio 代码上编写了我的 C# 逻辑。我正在尝试添加一个配置文件来配置某些参数,但是,一旦我创建了 appsettings.json 文件,我就无法在代码中访问它。需要帮助才能访问配置文件中的值。

using System;
using System.IO;
using Microsoft.Extensions.Configuration;

namespace testapp
{
    class Program
    {
        static void Main(string[] args)
        {
        var appSettings = new Config();
           var config = new ConfigurationBuilder()
             .SetBasePath(Directory.GetCurrentDirectory())
             .AddEnvironmentVariables() //This line doesnt compile 
             .AddJsonFile("appsettings.json", true, true)
             .Build();
           config.Bind(appSettings);       //This line doesnt compile        

            Console.WriteLine("Hello World!");
            Console.WriteLine($" Hello {appSettings.name} !"); 
        }
    }
    public class Config
    {
       public string name{ get; set; }
    }
}

【问题讨论】:

  • 显示你的appsettings.json
  • { "name" : "Gary" }

标签: c# .net-core visual-studio-code app-config


【解决方案1】:

试试这个 -

using System;
using Microsoft.Extensions.Configuration;

namespace testapp
{
    class Program
    {            
        static void Main(string[] args)
        {
           var appSettings = new Config();
           var config = new ConfigurationBuilder()
             .SetBasePath(Directory.GetCurrentDirectory())
             .AddEnvironmentVariables()
             .AddJsonFile("appsettings.json", true, true)
             .Build();
           config.Bind(appSettings); 

           Console.WriteLine($" Hello {appSettings.name} !");
        }
    }

    public class Config
    {
       public string name{ get; set; }
    }
}

appsettings.json

{
    "name": "appName"
}

【讨论】:

  • 试过了,但代码无法编译,因为 IConfigurationRoot 不包含 Bind 方法的定义。
  • 使用这个var config = new ConfigurationBuilder()创建ConfigurationBuilder的对象
  • 我按照建议做了,但仍然无法编译。你能在最后编译它吗?
  • 使用系统;使用 Microsoft.Extensions.Configuration;命名空间 testapp { 类程序 { static void Main(string[] args) { var appSettings = new Config(); var config = new ConfigurationBuilder() .AddJsonFile("appsettings.json", true, true) .Build(); config.Bind(appSettings); Console.WriteLine($" 你好 {config.name} !"); } } 公共类配置 { 公共字符串名称 { 获取;放; } } }
  • 我已经编辑了我的答案,basepathAddEnvironmentVariables 不见了。你现在可以试试。
【解决方案2】:

HomeController.cs

    public class HomeController : Controller {

       private readonly IConfiguration _config;

       public HomeController(IConfiguration config) { _config = config; }

       public IActionResult Index() {

          Console.WriteLine(_config.GetSection("AppSettings:Token").Value);

          return View();

           }
   }

appsettings.json

{
  "AppSettings": {
    "Token": "T21pZC1NaXJ6YWVpWhithOutStar*"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

【讨论】:

    猜你喜欢
    • 2011-10-10
    • 1970-01-01
    • 2013-01-31
    • 2010-11-08
    • 2019-11-20
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多