不确定我在这里错过了什么,但我无法从我的应用程序设置中获得值。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总是空的。我是不是遗漏了什么?
在我的例子中,它就像在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"
}
}
我在WPF中遇到了类似的问题。NET Framework 5.0)
我所要做的就是登记。
services.AddSingleton<IConfiguration>(_configuration);
配置本身是这样配置的(在App.xaml.cs中):
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
_configuration = builder.Build();
假设在appsettings.json中有这样的值。
"MyValues": {
"Value1": "Xyz"
}
方法一:不进行依赖注入
在.cs文件中:
static IConfiguration conf = (new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json").Build());
public static string myValue1= conf["MyValues:Value1"].ToString();
方法二:依赖注入(推荐)
在Startup.cs文件:
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
...
services.AddServices(Configuration);
}
在你的控制器中:
public class TestController : ControllerBase
{
private string myValue1 { get; set; }
public TestController(IConfiguration configuration)
{
this.myValue1 = configuration.GetValue<string>("MyValues:Value1");
}
}
亚比谢克的回答是:
如果你想将值导入到静态类中,那么只需使用(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"
}
}