不确定我在这里错过了什么,但我无法从我的应用程序设置中获得值。Json在我的。net核心应用程序。我有我的appsettings。json:

{
    "AppSettings": {
        "Version": "One"
    }
}

启动:

public class Startup
{
    private IConfigurationRoot _configuration;
    public Startup(IHostingEnvironment env)
    {
        _configuration = new ConfigurationBuilder()
    }
    public void ConfigureServices(IServiceCollection services)
    {
      //Here I setup to read appsettings        
      services.Configure<AppSettings>(_configuration.GetSection("AppSettings"));
    }
}

模型:

public class AppSettings
{
    public string Version{ get; set; }
}

控制器:

public class HomeController : Controller
{
    private readonly AppSettings _mySettings;

    public HomeController(IOptions<AppSettings> settings)
    {
        //This is always null
        _mySettings = settings.Value;
    }
}

_mySettings总是空的。我是不是遗漏了什么?


当前回答

在Startup类的构造函数中,可以访问appsettings。json和使用注入的IConfiguration对象的许多其他设置:

Startup.cs构造函数

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;

        //here you go
        var myvalue = Configuration["Grandfather:Father:Child"];

    }

public IConfiguration Configuration { get; }

json的内容

  {
  "Grandfather": {
    "Father": {
      "Child": "myvalue"
    }
  }

其他回答

我认为最好的选择是:

创建一个模型类作为配置模式 在DI注册: services.Configure (Configuration.GetSection(“democonfig”)); 从控制器中的DI中获取模型对象的值: private readonly myConfig; (IOptions<your_model> configOps) { 这一点。myConfig = configOps.Value; }

appsetting.json


{   
  "Settings": {      
    "ProjectName": "Sample Project"   
  }
}

定义一个具有相同属性名的类:

 public class Settings
 {
     public string ProjectName { get; set; }  
 }

在Startup.cs中添加配置:

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

注入控制器:

public class TestController : Controller
{
    private readonly Settings _settings;       
    
    public TestController(IOptions<Settings> settings)
    {
         _settings = settings.Value;
    }
       
    [AllowAnonymous]
    public async Task<IActionResult> test()
    {
        var _projectname = _settings.ProjectName;
                
        return View();
    }
}

在我的例子中,它就像在Configuration对象上使用Bind()方法一样简单。然后将对象作为单例添加到DI中。

var instructionSettings = new InstructionSettings();
Configuration.Bind("InstructionSettings", instructionSettings);
services.AddSingleton(typeof(IInstructionSettings), (serviceProvider) => instructionSettings);

Instruction对象可以非常复杂。

{  
 "InstructionSettings": {
    "Header": "uat_TEST",
    "SVSCode": "FICA",
    "CallBackUrl": "https://UATEnviro.companyName.co.za/suite/webapi/receiveCallback",
    "Username": "s_integrat",
    "Password": "X@nkmail6",
    "Defaults": {
    "Language": "ENG",
    "ContactDetails":{
       "StreetNumber": "9",
       "StreetName": "Nano Drive",
       "City": "Johannesburg",
       "Suburb": "Sandton",
       "Province": "Gauteng",
       "PostCode": "2196",
       "Email": "ourDefaultEmail@companyName.co.za",
       "CellNumber": "0833 468 378",
       "HomeNumber": "0833 468 378",
      }
      "CountryOfBirth": "710"
    }
  }

. net core 3。X

不需要创建新的模型并在Startup.cs中设置。

控制器 添加新包-使用Microsoft.Extensions.Configuration;

public class HomeController : Controller
{
    private readonly IConfiguration _mySettings;

    public HomeController (IConfiguration mySettings)
    {
         _mySettings= mySettings;
    }
 
    //ex: you can get value on below function 
    public IEnumerable<string> Get()
    {
        var result = _config.GetValue<string>("AppSettings:Version"); // "One"
        return new string[] { result.ToString() };
    }
}

亚比谢克的回答是:

如果你想将值导入到静态类中,那么只需使用(ReSharper推荐):

static IConfiguration conf = (JsonConfigurationExtensions.AddJsonFile(new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()), "appsettings.json").Build());

private static string AuthorizationServiceURL { get; } = conf["ServiceUrls:AuthorizationUrl"];

// appsettings.json
{
  "ServiceUrls": {
    "AuthorizationUrl": "https://example.com/authorize"
  } 
}